package com.ts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ts.service.FileService;
import com.ts.service.FileTypeService;
import com.ts.service.FolderService;
import com.ts.vo.FileInfo;
import com.ts.vo.FileType;
import com.ts.vo.Folder;
import com.ts.vo.Oafile;
import com.ts.vo.Page;
import com.ts.vo.Users;

public class FileAction extends ActionSupport {
	private FileInfo fileinfo;
	private Oafile file;
	private Folder folder;
	private int currPage;
	private int targetfolderid;
	private int deleteindex;
	private String flag;
	//--------------------------------------
	private FileService fileService;
	private FolderService folderService;
	private FileTypeService typeService;
	//=========================================
	public String listRootFolderFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		List<Folder> folderlist=folderService.listFolder(currUser);
		if(folderlist.size()==0){//当该用户第一次登录时 自动创建一个文件夹,并以根文件夹为父文件夹
			System.out.println("Here");
			Folder pf=new Folder();
			pf.setFoldername("根文件夹");
			pf.setIsshare(0);
			pf.setUsers(currUser);
			pf.setPfolder(folderService.getFolderById(0));
			folderService.addFolder(pf);
			folderlist=folderService.listFolder(currUser, folderService.getFolderById(0));
		}
		folder=folderService.getMyRootFolder(currUser);//第一次进入根文件夹
		
		//查找出根文件夹下的文件
		int counts=fileService.getFileCount(currUser, folder);
		int pageSize=10;
		Page page = new Page(counts, pageSize, currPage);
		List<Oafile> filelist=fileService.listFile(currUser, folder, page);
		System.out.println("filelist");
		folder=folderService.getFolderById(folder.getFolderid());
		
		session.setAttribute("folderlist", folderlist);
		session.setAttribute("filelist", filelist);
		session.setAttribute("folder", folder);
		session.setAttribute("page", page);
		session.setAttribute("currPage", currPage);
		return "gotoFileTree";
	}
	public String listFileByFolderFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		System.out.println(folder);
		if(flag==null){//如果是翻页请求 ,则从session中取得标记 判断是 share查询  还是 my查询
			flag=(String)session.getAttribute("flag");
		}
		if(folder==null){//如果是文件操作的请求,没有从表单输入folder  则从session取值
			folder=(Folder)session.getAttribute("folder");
		}else{
			folder=folderService.getFolderById(folder.getFolderid());//通过表单 得到的文件夹			
		}
		//查找出该文件夹下的文件
		int counts=fileService.getFileCount(currUser, folder);
		int pageSize=10;
		Page page = new Page(counts, pageSize, currPage);
		List<Oafile> filelist=fileService.listFile(currUser, folder, page);
		session.setAttribute("filelist", filelist);
		session.setAttribute("folder", folder);
		session.setAttribute("page", page);
		session.setAttribute("currPage", currPage);
		session.setAttribute("flag", flag);
		System.out.println(flag);
		if("share".equals(flag)){
			return "gotoShareTree";
		}else{
			return "gotoFileTree";			
		}
	}
	public String listShareFoldersFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		List<Folder> folderlist=folderService.listSharedFolder();
		for(Folder temp:folderlist){
			System.out.println(temp.getFoldername());
		}
		session.setAttribute("folderlist", folderlist);
		session.removeAttribute("filelist");
		return "gotoShareTree";
	}
	//===================file CUD=================================================
	public String moveFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		file=fileService.getFileById(file.getFileid());
		file.setFolder(folderService.getFolderById(targetfolderid));
		if(fileService.editFile(file)){
			return "listFileByFolder";
		}else{
			session.setAttribute("errorinfo", "操作失败了");
			return "error";
		}
	
	}
	public String assignUnShareFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		folder=folderService.getFolderById(folder.getFolderid());
		folder.setIsshare(0);
		if(folderService.editFolder(folder)){
			System.out.println("取消文件夹共享");
			session.setAttribute("folder", folder);
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
		return null;
		
	}
	public String assignShareFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		folder=folderService.getFolderById(folder.getFolderid());
		folder.setIsshare(1);
		if(folderService.editFolder(folder)){
			System.out.println("设置文件夹共享");
			session.setAttribute("folder", folder);
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
		return null;
		
	}
	public String deleteFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		List<Oafile> filelist=(List<Oafile>)session.getAttribute("filelist");
		Oafile dfile=filelist.get(deleteindex);
		if(fileService.deleteFile(dfile)){
			filelist.remove(deleteindex);
			session.setAttribute("filelist", filelist);
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
		return null;
	}
	public String uploadFileAction() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload_files/"+fileinfo.getUploadFileName());
		String filename=fileinfo.getUploadFileName();
		String extension=filename.substring(filename.lastIndexOf(".")+1);
		Folder currfolder=(Folder)session.getAttribute("folder");
		File f = fileinfo.getUpload();
		FileInputStream fis = null;
		FileOutputStream fos = null;
		double fileSize=0;
		try {
			fis = new FileInputStream(f);
			byte[] b = new byte[fis.available()];
			fileSize=b.length;
			fis.read(b);
			fis.close();
			fos = new FileOutputStream(savePath);
			fos.write(b);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		file=new Oafile();
		file.setFilecontent(savePath);
		file.setFilename(fileinfo.getUploadFileName());
		file.setFsize(fileSize);
		file.setUsers(currUser);
		file.setFolder(currfolder);
		FileType filetype=typeService.getFileTypeByExt(extension);
		if(filetype==null){//当上传到的文件类型数据库中没有时,自动新增一个类型
			FileType newtype=new FileType();
			newtype.setExtension(extension);
			newtype.setTypedesc(fileinfo.getUploadContentType());
			newtype.setTypename(fileinfo.getUploadContentType());
			typeService.addFileType(newtype);
			filetype=typeService.getFileTypeByExt(newtype.getExtension());
		}
		file.setFiletype(filetype);
		if(fileService.addFile(file)){
			return "listFileByFolder";
		}else{
			return "error";
		}

	}
	//============folder CUD===================================
	public String newFolderFileAction() throws Exception{
		HttpSession session = ServletActionContext.getRequest().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		Users currUser = (Users) session.getAttribute("user");
		folder.setIsshare(0);
		folder.setUsers(currUser);
		folder.setPfolder(folderService.getFolderById(folder.getPfolder().getFolderid()));
		if(folderService.addFolder(folder)){
			folder=folderService.getFolderByName(folder.getFoldername());
			session.setAttribute("folder", folder);
			return "listFileByFolder";
		}else{
			session.setAttribute("errorinfo", "新建文件夹失败了");
			return "error";
		}
	}
	private static final long serialVersionUID = 1L;
	public FileInfo getFileinfo() {
		return fileinfo;
	}
	public void setFileinfo(FileInfo fileinfo) {
		this.fileinfo = fileinfo;
	}
	public FileService getFileService() {
		return fileService;
	}
	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
	public FolderService getFolderService() {
		return folderService;
	}
	public void setFolderService(FolderService folderService) {
		this.folderService = folderService;
	}
	public Oafile getFile() {
		return file;
	}
	public void setFile(Oafile file) {
		this.file = file;
	}
	public Folder getFolder() {
		return folder;
	}
	public void setFolder(Folder folder) {
		this.folder = folder;
	}

	public FileTypeService getTypeService() {
		return typeService;
	}

	public void setTypeService(FileTypeService typeService) {
		this.typeService = typeService;
	}
	public int getCurrPage() {
		return currPage;
	}
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
	public int getTargetfolderid() {
		return targetfolderid;
	}
	public void setTargetfolderid(int targetfolderid) {
		this.targetfolderid = targetfolderid;
	}
	public int getDeleteindex() {
		return deleteindex;
	}
	public void setDeleteindex(int deleteindex) {
		this.deleteindex = deleteindex;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}
最近下载更多
微信网友_5963854197018624  LV7 2023年4月6日
akbar2020  LV9 2022年9月4日
19857460815  LV1 2022年6月20日
zeze2580  LV2 2022年5月9日
wanglinddad  LV55 2022年3月26日
habiya  LV3 2022年3月14日
ds9527  LV2 2022年3月3日
MarkLee 琥珀川  LV13 2022年1月21日
小豆芽  LV8 2021年6月10日
xufei123  LV4 2021年6月2日
最近浏览更多
张泽帅  LV6 10月22日
krispeng  LV13 8月12日
9605451tjb  LV4 8月7日
yunsgui  LV1 6月25日
liyan54188  LV2 6月25日
暂无贡献等级
26737721  LV4 5月27日
jhyan127 2月18日
暂无贡献等级
米继宝是憨憨  LV4 1月29日
森sdfgf  LV8 1月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友