use of com.qiwenshare.common.exception.QiwenException in project qiwen-file by qiwenshare.
the class FileController method updateFile.
@Operation(summary = "修改文件", description = "支持普通文本类文件的修改", tags = { "file" })
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> updateFile(@RequestBody UpdateFileDTO updateFileDTO) {
JwtUser sessionUserBean = SessionUtil.getSession();
UserFile userFile = userFileService.getById(updateFileDTO.getUserFileId());
FileBean fileBean = fileService.getById(userFile.getFileId());
Long pointCount = fileService.getFilePointCount(userFile.getFileId());
if (pointCount > 1) {
return RestResult.fail().message("暂不支持修改");
}
String content = updateFileDTO.getFileContent();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
try {
Writer writer1 = ufopFactory.getWriter(fileBean.getStorageType());
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileBean.getFileUrl());
int fileSize = byteArrayInputStream.available();
writeFile.setFileSize(fileSize);
writer1.write(byteArrayInputStream, writeFile);
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = ufopFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
fileBean.setIdentifier(md5Str);
fileBean.setModifyTime(DateUtil.getCurrentTime());
fileBean.setModifyUserId(sessionUserBean.getUserId());
fileBean.setFileSize((long) fileSize);
fileService.updateById(fileBean);
} catch (Exception e) {
throw new QiwenException(999999, "修改文件异常");
} finally {
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return RestResult.success().message("修改文件成功");
}
use of com.qiwenshare.common.exception.QiwenException in project qiwen-file by qiwenshare.
the class UserFileService method updateFilepathByFilepath.
@Override
public void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName, long userId) {
List<UserFile> userFileList = selectUserFileListByPath(newfilePath, userId);
List<UserFile> userFileNameList = userFileList.stream().filter(o -> o.getFileName().equals(fileName) && o.getExtendName().equals(extendName)).collect(Collectors.toList());
if (userFileNameList != null && userFileNameList.size() > 0) {
throw new QiwenException(200000, "目的路径同名文件已存在,不能移动");
}
// 移动根目录
userFileMapper.updateFilepathByPathAndName(oldfilePath, newfilePath, fileName, extendName, userId);
// 移动子目录
oldfilePath = oldfilePath + fileName + "/";
newfilePath = newfilePath + fileName + "/";
if (StringUtils.isEmpty(extendName)) {
// 为空说明是目录,则需要移动子目录
List<UserFile> list = selectFileListLikeRightFilePath(oldfilePath, userId);
for (UserFile newUserFile : list) {
newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath, newfilePath));
userFileMapper.updateById(newUserFile);
}
}
}
use of com.qiwenshare.common.exception.QiwenException in project qiwen-file by qiwenshare.
the class JwtAuthenticationTokenFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
String version = sysParamService.getValue("version");
if (!qiwenVersion.equals(version)) {
throw new QiwenException(999999, "脚本未初始化,请在数据库执行数据初始化脚本,存放路径: '/resources/import.sql'!");
}
String token = request.getHeader("token");
if (StringUtils.isNotBlank(token) && !"undefined".equals(token)) {
Long userId = userService.getUserIdByToken(token);
// 验证
if (userId != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = userService.loadUserByUsername(String.valueOf(userId));
if (userDetails.isEnabled()) {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
}
}
}
chain.doFilter(request, response);
}
use of com.qiwenshare.common.exception.QiwenException in project qiwen-file by qiwenshare.
the class FileService method unzipFile.
@Override
public void unzipFile(long userFileId, int unzipMode, String filePath) {
UserFile userFile = userFileMapper.selectById(userFileId);
FileBean fileBean = fileMapper.selectById(userFile.getFileId());
File destFile = new File(UFOPUtils.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl());
Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
downloadFile.setFileSize(fileBean.getFileSize());
InputStream inputStream = downloader.getInputStream(downloadFile);
try {
FileUtils.copyInputStreamToFile(inputStream, destFile);
} catch (IOException e) {
e.printStackTrace();
}
String extendName = userFile.getExtendName();
String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + extendName, "");
List<String> fileEntryNameList = new ArrayList<>();
if ("zip".equals(extendName)) {
fileEntryNameList = FileOperation.unzip(destFile, unzipUrl);
} else if ("rar".equals(extendName)) {
try {
fileEntryNameList = FileOperation.unrar(destFile, unzipUrl);
} catch (Exception e) {
e.printStackTrace();
log.error("rar解压失败" + e);
throw new QiwenException(500001, "rar解压异常:" + e.getMessage());
}
} else {
throw new QiwenException(500002, "不支持的文件格式!");
}
if (destFile.exists()) {
destFile.delete();
}
if (!fileEntryNameList.isEmpty() && unzipMode == 1) {
UserFile qiwenDir = QiwenFileUtil.getQiwenDir(userFile.getUserId(), userFile.getFilePath(), userFile.getFileName());
userFileMapper.insert(qiwenDir);
}
for (int i = 0; i < fileEntryNameList.size(); i++) {
String entryName = fileEntryNameList.get(i);
asyncTaskComp.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath);
}
}
Aggregations