@Override public FileEntity saveFile(String filename, byte[] data) { try { String path = FolderUtil.getFilePath(filename); String name = FolderUtil.getFileName(filename); FolderEntity folder = getFolderBusiness().createFolder(path); FileEntity file = getDao().getFileDao().getByName(folder.getId(), name); if (file == null) { file = new FileEntity( name, name, folder.getId(), MimeType.getContentTypeByExt(FolderUtil.getFileExt(filename)), new Date(), data.length); } getDao().getFileDao().save(file, data); getSystemService().getFileCache().remove(filename); return file; } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public FileEntity findFile(String filename) { try { String path = FolderUtil.getFilePath(filename); String name = FolderUtil.getFileName(filename); TreeItemDecorator<FolderEntity> folder = getFolderBusiness().findFolderByPath(getFolderBusiness().getTree(), path); if (folder == null) { return null; } return getDao().getFileDao().getByName(folder.getEntity().getId(), name); } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public byte[] readFile(String filename) { try { String path = FolderUtil.getFilePath(filename); String name = FolderUtil.getFileName(filename); FolderEntity folder = getFolderBusiness().findFolderByPath(getFolderBusiness().getTree(), path).getEntity(); if (folder == null) { return null; } FileEntity file = getDao().getFileDao().getByName(folder.getId(), name); if (file != null) { return getDao().getFileDao().getFileContent(file); } return null; } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public void remove(String filename) { String path = FolderUtil.getFilePath(filename); String name = FolderUtil.getFileName(filename); TreeItemDecorator<FolderEntity> folder = getFolderBusiness().findFolderByPath(getFolderBusiness().getTree(), path); if (folder == null) { logger.error("Folder not found. " + path); return; } FileEntity file = getDao().getFileDao().getByName(folder.getEntity().getId(), name); if (file == null) { logger.error("File not found. " + filename); return; } FolderPermissionEntity perm = getFolderPermissionBusiness() .getPermission(folder.getEntity(), VosaoContext.getInstance().getUser()); if (perm.isChangeGranted()) { getDao().getFileDao().remove(file.getId()); getSystemService().getFileCache().remove(filename); } }