Search in sources :

Example 1 with QiwenException

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("修改文件成功");
}
Also used : WriteFile(com.qiwenshare.ufop.operation.write.domain.WriteFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) ByteArrayInputStream(java.io.ByteArrayInputStream) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) JwtUser(com.qiwenshare.common.util.security.JwtUser) Writer(com.qiwenshare.ufop.operation.write.Writer) Operation(io.swagger.v3.oas.annotations.Operation)

Example 2 with QiwenException

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);
        }
    }
}
Also used : LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QiwenException(com.qiwenshare.common.exception.QiwenException) UserFile(com.qiwenshare.file.domain.UserFile) StringUtils(org.apache.commons.lang3.StringUtils) RecoveryFile(com.qiwenshare.file.domain.RecoveryFile) Service(org.springframework.stereotype.Service) RandomUtil(cn.hutool.core.util.RandomUtil) RecoveryFileMapper(com.qiwenshare.file.mapper.RecoveryFileMapper) IUserFileService(com.qiwenshare.file.api.IUserFileService) UFOPUtils(com.qiwenshare.ufop.util.UFOPUtils) ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) FileTypeMapper(com.qiwenshare.file.mapper.FileTypeMapper) UserFileMapper(com.qiwenshare.file.mapper.UserFileMapper) DateUtil(com.qiwenshare.common.util.DateUtil) Executor(java.util.concurrent.Executor) Resource(javax.annotation.Resource) FileConstant(com.qiwenshare.common.constant.FileConstant) RestResult(com.qiwenshare.common.result.RestResult) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) FileListVo(com.qiwenshare.file.vo.file.FileListVo) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SessionUtil(com.qiwenshare.common.util.security.SessionUtil) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) JwtUser(com.qiwenshare.common.util.security.JwtUser) IPage(com.baomidou.mybatisplus.core.metadata.IPage) FileMapper(com.qiwenshare.file.mapper.FileMapper) Transactional(org.springframework.transaction.annotation.Transactional) UserFile(com.qiwenshare.file.domain.UserFile) QiwenException(com.qiwenshare.common.exception.QiwenException)

Example 3 with QiwenException

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);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) QiwenException(com.qiwenshare.common.exception.QiwenException) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource)

Example 4 with QiwenException

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);
    }
}
Also used : DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Downloader(com.qiwenshare.ufop.operation.download.Downloader) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) UserFile(com.qiwenshare.file.domain.UserFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) File(java.io.File)

Aggregations

QiwenException (com.qiwenshare.common.exception.QiwenException)4 UserFile (com.qiwenshare.file.domain.UserFile)3 JwtUser (com.qiwenshare.common.util.security.JwtUser)2 FileBean (com.qiwenshare.file.domain.FileBean)2 DownloadFile (com.qiwenshare.ufop.operation.download.domain.DownloadFile)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RandomUtil (cn.hutool.core.util.RandomUtil)1 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 ServiceImpl (com.baomidou.mybatisplus.extension.service.impl.ServiceImpl)1 FileConstant (com.qiwenshare.common.constant.FileConstant)1 RestResult (com.qiwenshare.common.result.RestResult)1 DateUtil (com.qiwenshare.common.util.DateUtil)1 SessionUtil (com.qiwenshare.common.util.security.SessionUtil)1 IUserFileService (com.qiwenshare.file.api.IUserFileService)1 RecoveryFile (com.qiwenshare.file.domain.RecoveryFile)1 FileMapper (com.qiwenshare.file.mapper.FileMapper)1