package com.hdfs.file.serviceImpl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import com.encryption.DesEncrypter;
import com.encryption.RSAEncrypter;
import com.hdfs.comm.util.fileUtil;
import com.hdfs.comm.util.pathToId;
import com.hdfs.file.bean.HdfsFile;
import com.hdfs.file.bean.HdfsMemory;
import com.hdfs.file.bean.checkFile;
import com.hdfs.file.bean.dillResult;
import com.hdfs.file.dao.fileDao;
import com.hdfs.file.service.fileService;
import com.hdfs.user.bean.Users;
import com.hdfs.user.dao.userDao;
import com.hdfsTools.action.createAction;
import com.hdfsTools.action.deleteAction;
import com.hdfsTools.action.downLoadAction;
import com.hdfsTools.action.renameAction;
import com.hdfsTools.action.upLoadAction;
public class fileServiceImpl implements fileService {
public fileDao filedao;
public userDao userdao;
public userDao getUserdao() {
return userdao;
}
public void setUserdao(userDao userdao) {
this.userdao = userdao;
}
@Override
public String listFile(long currentId, long userId) {
List<HdfsFile> fileList = filedao.listFile(currentId, userId);// 获得文件列表
// 转化为json格式
ArrayList<checkFile> list = new ArrayList();
for (HdfsFile hdfsfile : fileList) {
checkFile file = new checkFile();
if (null == hdfsfile.getCreateTime()) {
file.setCreateTime("");
} else {
file.setCreateTime(hdfsfile.getCreateTime().toString());
}
if (null == hdfsfile.getDeadline()) {
file.setTime("");
} else {
file.setTime(hdfsfile.getDeadline().toString());
}
file.setId(hdfsfile.getFileId());
if (null == hdfsfile.getModifiedTime()) {
file.setModifiedTime("");
} else {
file.setModifiedTime(hdfsfile.getModifiedTime().toString());
}
file.setName(hdfsfile.getFileName());
file.setParentId(hdfsfile.getParentid());
if (null == hdfsfile.getSafeLevel()) {
file.setSaveLevel(0);
} else {
file.setSaveLevel(hdfsfile.getSafeLevel());
}
if (null == hdfsfile.getSize()) {
file.setSize(-1);
} else {
file.setSize(hdfsfile.getSize());
}
file.setType(hdfsfile.getType());
file.setUrl(hdfsfile.getFileUrl());
list.add(file);
file = null;
}
JSONObject jsonresult = new JSONObject();
jsonresult.accumulate("list", list);
String jsonList = JSONUtils.valueToString(jsonresult);
System.out.println(jsonList);
return jsonList;
}
@Override
public String searchfile(String name, long userId) {
List<HdfsFile> fileList = filedao.SearchFile(name, userId);// 获得文件列表
// 转化为json格式
ArrayList<checkFile> list = new ArrayList();
System.out.println(fileList.toString());
for (HdfsFile hdfsfile : fileList) {
checkFile file = new checkFile();
if (null == hdfsfile.getCreateTime()) {
file.setCreateTime("");
} else {
file.setCreateTime(hdfsfile.getCreateTime().toString());
}
if (null == hdfsfile.getDeadline()) {
file.setTime("");
} else {
file.setTime(hdfsfile.getDeadline().toString());
}
file.setId(hdfsfile.getFileId());
if (null == hdfsfile.getModifiedTime()) {
file.setModifiedTime("");
} else {
file.setModifiedTime(hdfsfile.getModifiedTime().toString());
}
file.setName(hdfsfile.getFileName());
file.setParentId(hdfsfile.getParentid());
if (null == hdfsfile.getSafeLevel()) {
file.setSaveLevel(0);
} else {
file.setSaveLevel(hdfsfile.getSafeLevel());
}
if (null == hdfsfile.getSize()) {
file.setSize(-1);
} else {
file.setSize(hdfsfile.getSize());
}
file.setType(hdfsfile.getType());
file.setUrl(hdfsfile.getFileUrl());
list.add(file);
file = null;
}
JSONObject jsonresult = new JSONObject();
jsonresult.accumulate("list", list);
String jsonList = JSONUtils.valueToString(jsonresult);
System.out.println(jsonList);
return jsonList;
}
@Override
public List<HdfsFile> listAllFile() {
List<HdfsFile> fileList = filedao.listAllFile();
return fileList;
}
public fileDao getFiledao() {
return filedao;
}
public void setFiledao(fileDao filedao) {
this.filedao = filedao;
}
@Override
public Boolean mkdir(long parentId, String name, long userId)
throws IOException {
String parentPath = filedao.getFileUrl(parentId);// 获取父目录的id
// System.out.println("the filepath is:"+parentPath);
// String filePath=parentPath.substring(1, parentPath.length());
System.out.println("the filepath is:" + parentPath);
String newfile = null;
System.out.println(parentPath.length());
System.out.println(parentPath.lastIndexOf("/"));
if ((parentPath.length() - 1) == parentPath.lastIndexOf("/")) {
newfile = parentPath + name;
}// 新建文件的详细路径
else {
newfile = parentPath + "/" + name;
}
long fileId = pathToId.ParsepathToId(newfile);// hash得出fileid
HdfsFile hdfsfile = new HdfsFile(fileId, name, parentId, newfile, 0);
hdfsfile.setUserId(userId);
hdfsfile.setCreateTime(new Date());
hdfsfile.setModifiedTime(new Date());
boolean result = filedao.insertFile(hdfsfile);// 保存文件
createAction caction = new createAction();
caction.createDir(newfile);// 操作文件系统
return result;
}
@Override
public Boolean rootmkdir(long parentId, String name, long userId,
long rootid) throws IOException {
String parentPath = filedao.getFileUrl(parentId);// 获取父目录的id
// System.out.println("the filepath is:"+parentPath);
// String filePath=parentPath.substring(1, parentPath.length());
System.out.println("the filepath is:" + parentPath);
// newfile指向文件的完整路径
String newfile = null;
System.out.println(parentPath.length());
System.out.println(parentPath.lastIndexOf("/"));
if ((parentPath.length() - 1) == parentPath.lastIndexOf("/")) {
newfile = parentPath + name;
}// 新建文件的详细路径
else {
newfile = parentPath + "/" + name;
}
long fileId = rootid;// hash得出fileid
HdfsFile hdfsfile = new HdfsFile(fileId, name, parentId, newfile, 0);
hdfsfile.setUserId(userId);
hdfsfile.setCreateTime(new Date());
hdfsfile.setModifiedTime(new Date());
System.out.println(newfile);
boolean result = filedao.insertFile(hdfsfile);// 保存文件,即在数据库中保存该文件的信息
// 在hdfs文件系统创建相应的文件夹
createAction caction = new createAction();
caction.createDir(newfile);// 操作hdfs文件系统
return result;
}
@Override
public String deleteFile(long fileId, HdfsMemory memory) {
// TODO Auto-generated method stub
boolean deletedb = false;
String result = "";
HdfsFile file = filedao.findFile(fileId);
if (filedao.findFile(fileId) == null)
System.out.println("kongduixiang");// 获得文件的url
System.out.println("url is :" + file.getFileUrl());
deleteAction delete = new deleteAction();
boolean deletehdfs = delete.deleteDirector(file.getFileUrl());
if (deletehdfs) {
int delmem = filedao.deleteFilesmem(file.getFileUrl());
deletedb = filedao.deleteFiles(file.getFileUrl());// 删除数据库中该文件和子文件的记