Search in sources :

Example 1 with NetworkDisk

use of com.moxi.mogublog.commons.entity.NetworkDisk in project mogu_blog_v2 by moxi624.

the class NetworkDiskServiceImpl method updateFilepathByFilepath.

@Override
public void updateFilepathByFilepath(NetworkDiskVO networkDiskVO) {
    String oldFilePath = networkDiskVO.getOldFilePath();
    String newFilePath = networkDiskVO.getNewFilePath();
    String fileName = networkDiskVO.getFileName();
    String fileOldName = networkDiskVO.getFileOldName();
    String extendName = networkDiskVO.getExtendName();
    if ("null".equals(networkDiskVO.getExtendName())) {
        extendName = null;
    }
    // 判断移动的路径是否相同【拼接出原始目录】
    String fileOldPath = oldFilePath + fileOldName + "/";
    if (fileOldPath.equals(newFilePath)) {
        throw new UpdateException("不能选择自己");
    }
    // 移动根目录
    QueryWrapper<NetworkDisk> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.FILE_PATH, networkDiskVO.getOldFilePath());
    queryWrapper.eq(SQLConf.FILE_NAME, networkDiskVO.getFileName());
    if (StringUtils.isNotEmpty(extendName)) {
        queryWrapper.eq(SQLConf.EXTEND_NAME, extendName);
    } else {
        queryWrapper.isNull(SQLConf.EXTEND_NAME);
    }
    List<NetworkDisk> networkDiskList = networkDiskService.list(queryWrapper);
    for (NetworkDisk networkDisk : networkDiskList) {
        // 修改新的路径
        networkDisk.setFilePath(newFilePath);
        // 修改旧文件名
        networkDisk.setFileOldName(networkDiskVO.getFileOldName());
        // 如果扩展名为空,代表是文件夹,还需要修改文件名
        if (StringUtils.isEmpty(extendName)) {
            networkDisk.setFileName(networkDiskVO.getFileOldName());
        }
    }
    if (networkDiskList.size() > 0) {
        networkDiskService.updateBatchById(networkDiskList);
    }
    // 移动子目录
    oldFilePath = oldFilePath + fileName + "/";
    newFilePath = newFilePath + fileOldName + "/";
    oldFilePath = oldFilePath.replace("\\", "\\\\\\\\");
    oldFilePath = oldFilePath.replace("'", "\\'");
    oldFilePath = oldFilePath.replace("%", "\\%");
    oldFilePath = oldFilePath.replace("_", "\\_");
    // 为null说明是目录,则需要移动子目录
    if (extendName == null) {
        // 移动根目录
        QueryWrapper<NetworkDisk> childQueryWrapper = new QueryWrapper<>();
        childQueryWrapper.likeRight(SQLConf.FILE_PATH, oldFilePath);
        List<NetworkDisk> childList = networkDiskService.list(childQueryWrapper);
        for (NetworkDisk networkDisk : childList) {
            String filePath = networkDisk.getFilePath();
            networkDisk.setFilePath(filePath.replace(oldFilePath, newFilePath));
        }
        if (childList.size() > 0) {
            networkDiskService.updateBatchById(childList);
        }
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UpdateException(com.moxi.mougblog.base.exception.exceptionType.UpdateException) NetworkDisk(com.moxi.mogublog.commons.entity.NetworkDisk)

Example 2 with NetworkDisk

use of com.moxi.mogublog.commons.entity.NetworkDisk in project mogu_blog_v2 by moxi624.

the class NetworkDiskServiceImpl method deleteFile.

@Override
public void deleteFile(NetworkDiskVO networkDiskVO, Map<String, String> qiNiuConfig) {
    String uid = networkDiskVO.getUid();
    if (StringUtils.isNotEmpty(uid)) {
        log.error("删除的文件不能为空");
    }
    NetworkDisk networkDisk = networkDiskService.getById(uid);
    String uploadLocal = qiNiuConfig.get(SysConf.UPLOAD_LOCAL);
    String uploadQiNiu = qiNiuConfig.get(SysConf.UPLOAD_QI_NIU);
    String uploadMinio = qiNiuConfig.get(SysConf.UPLOAD_MINIO);
    // 修改为删除状态
    networkDisk.setStatus(EStatus.DISABLED);
    networkDisk.updateById();
    // 判断删除的是文件 or 文件夹
    if (SysConf.ONE == networkDisk.getIsDir()) {
        // 删除的是文件夹,那么需要把文件下所有的文件获得,进行删除
        // 获取文件的路径,查询出该路径下所有的文件
        String path = networkDisk.getFilePath() + networkDisk.getFileName();
        QueryWrapper<NetworkDisk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        // 查询以  path%  开头的
        queryWrapper.likeRight(SQLConf.FILE_PATH, path);
        List<NetworkDisk> list = networkDiskService.list(queryWrapper);
        if (list.size() > 0) {
            // 将所有的状态设置成失效
            list.forEach(item -> {
                item.setStatus(EStatus.DISABLED);
            });
            Boolean isUpdateSuccess = networkDiskService.updateBatchById(list);
            if (isUpdateSuccess) {
                // 删除本地文件,同时移除本地文件
                if (EOpenStatus.OPEN.equals(uploadLocal)) {
                    // 获取删除的路径
                    List<String> fileList = new ArrayList<>();
                    list.forEach(item -> {
                        fileList.add(UPLOAD_PATH + item.getLocalUrl());
                    });
                    // 批量删除本地图片
                    MoGuFileUtil.deleteFileList(fileList);
                }
                // 删除七牛云上文件
                if (EOpenStatus.OPEN.equals(uploadQiNiu)) {
                    List<String> fileList = new ArrayList<>();
                    list.forEach(item -> {
                        fileList.add(item.getQiNiuUrl());
                    });
                    qiniuUtil.deleteFileList(fileList, qiNiuConfig);
                }
                // 删除Minio中的文件
                if (EOpenStatus.OPEN.equals(uploadMinio)) {
                    List<String> fileList = new ArrayList<>();
                    list.forEach(item -> {
                        fileList.add(item.getMinioUrl());
                    });
                    minioUtil.deleteBatchFile(fileList);
                }
            }
        }
    } else {
        // 删除本地文件,同时移除本地文件
        if (EOpenStatus.OPEN.equals(uploadLocal)) {
            String localUrl = networkDisk.getLocalUrl();
            MoGuFileUtil.deleteFile(UPLOAD_PATH + localUrl);
        }
        // 删除七牛云上文件
        if (EOpenStatus.OPEN.equals(uploadQiNiu)) {
            String qiNiuUrl = networkDisk.getQiNiuUrl();
            qiniuUtil.deleteFile(qiNiuUrl, qiNiuConfig);
        }
        // 删除Minio中的文件
        if (EOpenStatus.OPEN.equals(uploadMinio)) {
            String minioUrl = networkDisk.getMinioUrl();
            if (StringUtils.isNotEmpty(minioUrl)) {
                String[] minUrlArray = minioUrl.split("/");
                // 找到文件名
                minioUtil.deleteFile(minUrlArray[minUrlArray.length - 1]);
            } else {
                log.error("删除的文件不存在Minio文件地址");
            }
        }
        Storage storage = storageService.getStorageByAdmin();
        long storageSize = 0L;
        try {
            storageSize = storage.getStorageSize() - networkDisk.getFileSize();
        } catch (Exception e) {
            log.info("本地文件空间不存在!");
        }
        storage.setStorageSize(storageSize > 0 ? storageSize : 0L);
        storageService.updateById(storage);
    }
}
Also used : Storage(com.moxi.mogublog.commons.entity.Storage) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ArrayList(java.util.ArrayList) NetworkDisk(com.moxi.mogublog.commons.entity.NetworkDisk) UpdateException(com.moxi.mougblog.base.exception.exceptionType.UpdateException)

Example 3 with NetworkDisk

use of com.moxi.mogublog.commons.entity.NetworkDisk in project mogu_blog_v2 by moxi624.

the class StorageServiceImpl method uploadFile.

@Override
public String uploadFile(NetworkDisk networkDisk, List<MultipartFile> fileDatas) {
    HttpServletRequest request = RequestHolder.getRequest();
    SystemConfig systemConfig = feignUtil.getSystemConfig();
    if (systemConfig == null) {
        throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
    }
    // 计算文件大小
    Long newStorageSize = 0L;
    Long storageSize = 0L;
    for (MultipartFile fileData : fileDatas) {
        newStorageSize += fileData.getSize();
    }
    Storage storage = getStorageByAdmin();
    if (storage != null) {
        storageSize = storage.getStorageSize() + newStorageSize;
        // 判断上传的文件是否超过了剩余空间
        if (storage.getMaxStorageSize() < storageSize) {
            throw new UpdateException(ErrorCode.UPDATE_DEFAULT_ERROR, "上传失败,您可用的空间已经不足!");
        } else {
            storage.setStorageSize(storageSize);
        }
    } else {
        throw new UpdateException(ErrorCode.UPDATE_DEFAULT_ERROR, "上传失败,您没有分配可用的上传空间!");
    }
    // 上传文件
    String result = fileService.batchUploadFile(request, fileDatas, systemConfig);
    List<File> fileList = WebUtils.getAllList(result, File.class);
    List<NetworkDisk> networkDiskList = new ArrayList<>();
    for (File file : fileList) {
        NetworkDisk saveNetworkDisk = new NetworkDisk();
        saveNetworkDisk.setAdminUid(request.getAttribute(SysConf.ADMIN_UID).toString());
        saveNetworkDisk.setFilePath(networkDisk.getFilePath());
        saveNetworkDisk.setQiNiuUrl(file.getQiNiuUrl());
        saveNetworkDisk.setLocalUrl(file.getPicUrl());
        saveNetworkDisk.setMinioUrl(file.getMinioUrl());
        saveNetworkDisk.setFileSize(file.getFileSize());
        saveNetworkDisk.setFileName(file.getPicName());
        saveNetworkDisk.setExtendName(file.getPicExpandedName());
        saveNetworkDisk.setFileOldName(file.getFileOldName());
        saveNetworkDisk.setCreateTime(new Date());
        networkDiskList.add(saveNetworkDisk);
    }
    // 上传文件
    networkDiskService.saveBatch(networkDiskList);
    // 更新容量大小
    storageService.updateById(storage);
    return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
}
Also used : SystemConfig(com.moxi.mogublog.commons.entity.SystemConfig) ArrayList(java.util.ArrayList) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryException(com.moxi.mougblog.base.exception.exceptionType.QueryException) MultipartFile(org.springframework.web.multipart.MultipartFile) Storage(com.moxi.mogublog.commons.entity.Storage) UpdateException(com.moxi.mougblog.base.exception.exceptionType.UpdateException) File(com.moxi.mogublog.commons.entity.File) MultipartFile(org.springframework.web.multipart.MultipartFile) NetworkDisk(com.moxi.mogublog.commons.entity.NetworkDisk)

Example 4 with NetworkDisk

use of com.moxi.mogublog.commons.entity.NetworkDisk in project mogu_blog_v2 by moxi624.

the class NetworkDiskServiceImpl method selectFileList.

@Override
public List<NetworkDisk> selectFileList(NetworkDisk networkDisk) {
    ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attribute.getRequest();
    // 获取配置文件
    Map<String, String> qiNiuResultMap = feignUtil.getSystemConfigMap(request.getAttribute(SysConf.TOKEN).toString());
    String picturePriority = qiNiuResultMap.get(SysConf.PICTURE_PRIORITY);
    QueryWrapper<NetworkDisk> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SysConf.STATUS, EStatus.ENABLE);
    queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
    // 根据扩展名查找
    if (networkDisk.getFileType() != 0) {
        // 判断是否是其它文件
        if (FileUtil.OTHER_TYPE == networkDisk.getFileType()) {
            queryWrapper.notIn(SQLConf.EXTEND_NAME, FileUtil.getFileExtendsByType(networkDisk.getFileType()));
        } else {
            queryWrapper.in(SQLConf.EXTEND_NAME, FileUtil.getFileExtendsByType(networkDisk.getFileType()));
        }
    } else if (StringUtils.isNotEmpty(networkDisk.getFilePath())) {
        // 没有扩展名时,查找全部
        queryWrapper.eq(SQLConf.FILE_PATH, networkDisk.getFilePath());
    }
    List<NetworkDisk> list = networkDiskService.list(queryWrapper);
    list.forEach(item -> {
        if (EFilePriority.QI_NIU.equals(picturePriority)) {
            item.setFileUrl(qiNiuResultMap.get(SysConf.QI_NIU_PICTURE_BASE_URL) + item.getQiNiuUrl());
        } else if (EFilePriority.MINIO.equals(picturePriority)) {
            item.setFileUrl(qiNiuResultMap.get(SysConf.MINIO_PICTURE_BASE_URL) + item.getMinioUrl());
        } else {
            item.setFileUrl(qiNiuResultMap.get(SysConf.LOCAL_PICTURE_BASE_URL) + item.getLocalUrl());
        }
    });
    return list;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) NetworkDisk(com.moxi.mogublog.commons.entity.NetworkDisk)

Example 5 with NetworkDisk

use of com.moxi.mogublog.commons.entity.NetworkDisk in project mogu_blog_v2 by moxi624.

the class NetworkDiskRestApi method unzipFile.

/**
 * 解压文件
 *
 * @return
 */
@ApiOperation(value = "解压文件", notes = "解压文件")
@RequestMapping(value = "/unzipFile", method = RequestMethod.POST)
public String unzipFile(@RequestBody NetworkDisk networkDisk) {
    RequestHolder.checkLogin();
    String zipFileUrl = PathUtil.getStaticPath() + networkDisk.getFileUrl();
    File file = FileOperation.newFile(zipFileUrl);
    String unzipUrl = file.getParent();
    List<String> fileEntryNameList = FileOperation.unzip(file, unzipUrl);
    List<NetworkDisk> fileBeanList = new ArrayList<>();
    for (int i = 0; i < fileEntryNameList.size(); i++) {
        String entryName = fileEntryNameList.get(i);
        String totalFileUrl = unzipUrl + entryName;
        File currentFile = FileOperation.newFile(totalFileUrl);
        NetworkDisk tempFileBean = new NetworkDisk();
        tempFileBean.setCreateTime(new Date());
        tempFileBean.setAdminUid(SysConf.DEFAULT_UID);
        tempFileBean.setFilePath(FileUtil.pathSplitFormat(networkDisk.getFilePath() + entryName.replace(currentFile.getName(), "")));
        if (currentFile.isDirectory()) {
            tempFileBean.setIsDir(1);
            tempFileBean.setFileName(currentFile.getName());
        } else {
            tempFileBean.setIsDir(0);
            tempFileBean.setExtendName(FileUtil.getFileType(totalFileUrl));
            tempFileBean.setFileName(FileUtil.getFileNameNotExtend(currentFile.getName()));
            tempFileBean.setFileSize(currentFile.length());
            tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
        }
        fileBeanList.add(tempFileBean);
    }
    networkDiskService.batchInsertFile(fileBeanList);
    return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
}
Also used : File(java.io.File) NetworkDisk(com.moxi.mogublog.commons.entity.NetworkDisk) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NetworkDisk (com.moxi.mogublog.commons.entity.NetworkDisk)6 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 UpdateException (com.moxi.mougblog.base.exception.exceptionType.UpdateException)3 Storage (com.moxi.mogublog.commons.entity.Storage)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 File (com.moxi.mogublog.commons.entity.File)1 SystemConfig (com.moxi.mogublog.commons.entity.SystemConfig)1 TreeNode (com.moxi.mogublog.picture.entity.TreeNode)1 QueryException (com.moxi.mougblog.base.exception.exceptionType.QueryException)1 File (java.io.File)1 Date (java.util.Date)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1