Search in sources :

Example 1 with Storage

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

the class AdminServiceImpl method getList.

@Override
public String getList(AdminVO adminVO) {
    QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
    String pictureResult = null;
    if (StringUtils.isNotEmpty(adminVO.getKeyword())) {
        queryWrapper.like(SQLConf.USER_NAME, adminVO.getKeyword()).or().like(SQLConf.NICK_NAME, adminVO.getKeyword().trim());
    }
    Page<Admin> page = new Page<>();
    page.setCurrent(adminVO.getCurrentPage());
    page.setSize(adminVO.getPageSize());
    // 去除密码
    queryWrapper.select(Admin.class, i -> !i.getProperty().equals(SQLConf.PASS_WORD));
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    IPage<Admin> pageList = adminService.page(page, queryWrapper);
    List<Admin> list = pageList.getRecords();
    final StringBuffer fileUids = new StringBuffer();
    List<String> adminUidList = new ArrayList<>();
    list.forEach(item -> {
        if (StringUtils.isNotEmpty(item.getAvatar())) {
            fileUids.append(item.getAvatar() + SysConf.FILE_SEGMENTATION);
        }
        adminUidList.add(item.getUid());
    });
    Map<String, String> pictureMap = new HashMap<>(Constants.NUM_TEN);
    if (fileUids != null) {
        pictureResult = this.pictureFeignClient.getPicture(fileUids.toString(), SysConf.FILE_SEGMENTATION);
    }
    List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);
    picList.forEach(item -> {
        pictureMap.put(item.get(SQLConf.UID).toString(), item.get(SQLConf.URL).toString());
    });
    // 获取用户的网盘存储空间
    String storageListJson = pictureFeignClient.getStorageByAdminUid(adminUidList);
    List<Storage> storageList = webUtil.getList(storageListJson, Storage.class);
    Map<String, Storage> storageMap = new HashMap<>();
    storageList.forEach(item -> {
        storageMap.put(item.getAdminUid(), item);
    });
    for (Admin item : list) {
        Role role = roleService.getById(item.getRoleUid());
        item.setRole(role);
        // 获取图片
        if (StringUtils.isNotEmpty(item.getAvatar())) {
            List<String> pictureUidsTemp = StringUtils.changeStringToString(item.getAvatar(), SysConf.FILE_SEGMENTATION);
            List<String> pictureListTemp = new ArrayList<>();
            pictureUidsTemp.forEach(picture -> {
                if (pictureMap.get(picture) != null && pictureMap.get(picture) != "") {
                    pictureListTemp.add(pictureMap.get(picture));
                }
            });
            item.setPhotoList(pictureListTemp);
        }
        // 设置已用容量大小和最大容量
        Storage storage = storageMap.get(item.getUid());
        if (storage != null) {
            item.setStorageSize(storage.getStorageSize());
            item.setMaxStorageSize(storage.getMaxStorageSize());
        } else {
            // 如果没有,默认为0
            item.setStorageSize(0L);
            item.setMaxStorageSize(0L);
        }
    }
    return ResultUtil.successWithData(pageList);
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) OnlineAdmin(com.moxi.mogublog.commons.entity.OnlineAdmin) Admin(com.moxi.mogublog.commons.entity.Admin) Role(com.moxi.mogublog.commons.entity.Role) Storage(com.moxi.mogublog.commons.entity.Storage)

Example 2 with Storage

use of com.moxi.mogublog.commons.entity.Storage 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 Storage

use of com.moxi.mogublog.commons.entity.Storage 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 Storage

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

the class StorageServiceImpl method editStorageSize.

@Override
public String editStorageSize(String adminUid, Long maxStorageSize) {
    QueryWrapper<Storage> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.ADMIN_UID, adminUid);
    queryWrapper.last(SysConf.LIMIT_ONE);
    Storage storage = storageService.getOne(queryWrapper);
    if (storage == null) {
        // 如果没有分配容量,那么初始化一个为0的
        log.error("未分配存储空间,重新初始化网盘空间!");
        return initStorageSize(adminUid, maxStorageSize);
    } else {
        if (maxStorageSize < storage.getStorageSize()) {
            return ResultUtil.errorWithMessage("网盘容量不能小于当前已用空间");
        }
        storage.setMaxStorageSize(maxStorageSize);
        storage.updateById();
        return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
    }
}
Also used : Storage(com.moxi.mogublog.commons.entity.Storage) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)

Example 5 with Storage

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

the class StorageRestApi method getStorage.

/**
 * 查询当前用户存储信息
 *
 * @return
 */
@RequestMapping(value = "/getStorage", method = RequestMethod.GET)
public String getStorage() {
    RequestHolder.checkLogin();
    Storage storage = storageService.getStorageByAdmin();
    return ResultUtil.successWithData(storage);
}
Also used : Storage(com.moxi.mogublog.commons.entity.Storage)

Aggregations

Storage (com.moxi.mogublog.commons.entity.Storage)7 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)5 NetworkDisk (com.moxi.mogublog.commons.entity.NetworkDisk)2 UpdateException (com.moxi.mougblog.base.exception.exceptionType.UpdateException)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 Admin (com.moxi.mogublog.commons.entity.Admin)1 File (com.moxi.mogublog.commons.entity.File)1 OnlineAdmin (com.moxi.mogublog.commons.entity.OnlineAdmin)1 Role (com.moxi.mogublog.commons.entity.Role)1 SystemConfig (com.moxi.mogublog.commons.entity.SystemConfig)1 QueryException (com.moxi.mougblog.base.exception.exceptionType.QueryException)1 Date (java.util.Date)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1