Search in sources :

Example 1 with File

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

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

the class FileServiceImpl method getPicture.

@Override
public String getPicture(String fileIds, String code) {
    if (StringUtils.isEmpty(code)) {
        code = Constants.SYMBOL_COMMA;
    }
    if (StringUtils.isEmpty(fileIds)) {
        log.error(MessageConf.PICTURE_UID_IS_NULL);
        return ResultUtil.result(SysConf.ERROR, MessageConf.PICTURE_UID_IS_NULL);
    } else {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> changeStringToString = StringUtils.changeStringToString(fileIds, code);
        QueryWrapper<com.moxi.mogublog.commons.entity.File> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SQLConf.UID, changeStringToString);
        List<com.moxi.mogublog.commons.entity.File> fileList = fileService.list(queryWrapper);
        if (fileList.size() > 0) {
            for (com.moxi.mogublog.commons.entity.File file : fileList) {
                if (file != null) {
                    Map<String, Object> remap = new HashMap<>();
                    // 获取七牛云地址
                    remap.put(SysConf.QI_NIU_URL, file.getQiNiuUrl());
                    // 获取Minio对象存储地址
                    remap.put(SysConf.MINIO_URL, file.getMinioUrl());
                    // 获取本地地址
                    remap.put(SysConf.URL, file.getPicUrl());
                    // 后缀名,也就是类型
                    remap.put(SysConf.EXPANDED_NAME, file.getPicExpandedName());
                    remap.put(SysConf.FILE_OLD_NAME, file.getFileOldName());
                    // 名称
                    remap.put(SysConf.NAME, file.getPicName());
                    remap.put(SysConf.UID, file.getUid());
                    remap.put(SQLConf.FILE_OLD_NAME, file.getFileOldName());
                    list.add(remap);
                }
            }
        }
        return ResultUtil.result(SysConf.SUCCESS, list);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) File(com.moxi.mogublog.commons.entity.File) File(com.moxi.mogublog.commons.entity.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 3 with File

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

the class FileServiceImpl method batchUploadFile.

@Override
public String batchUploadFile(HttpServletRequest request, List<MultipartFile> filedatas, SystemConfig systemConfig) {
    String uploadQiNiu = systemConfig.getUploadQiNiu();
    String uploadLocal = systemConfig.getUploadLocal();
    String uploadMinio = systemConfig.getUploadMinio();
    // 判断来源
    String source = request.getParameter(SysConf.SOURCE);
    // 如果是用户上传,则包含用户uid
    String userUid = "";
    // 如果是管理员上传,则包含管理员uid
    String adminUid = "";
    // 项目名
    String projectName = "";
    // 模块名
    String sortName = "";
    // 判断图片来源
    if (SysConf.PICTURE.equals(source)) {
        // 当从vue-mogu-web网站过来的,直接从参数中获取
        userUid = request.getParameter(SysConf.USER_UID);
        adminUid = request.getParameter(SysConf.ADMIN_UID);
        projectName = request.getParameter(SysConf.PROJECT_NAME);
        sortName = request.getParameter(SysConf.SORT_NAME);
    } else if (SysConf.ADMIN.equals(source)) {
        // 当图片从mogu-admin传递过来的时候
        userUid = request.getAttribute(SysConf.USER_UID).toString();
        adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
        projectName = request.getAttribute(SysConf.PROJECT_NAME).toString();
        sortName = request.getAttribute(SysConf.SORT_NAME).toString();
    } else {
        userUid = request.getAttribute(SysConf.USER_UID).toString();
        adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
        projectName = request.getAttribute(SysConf.PROJECT_NAME).toString();
        sortName = request.getAttribute(SysConf.SORT_NAME).toString();
    }
    // projectName现在默认base
    if (StringUtils.isEmpty(projectName)) {
        projectName = "base";
    }
    // TODO 检测用户上传,如果不是网站的用户就不能调用
    if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
        return ResultUtil.result(SysConf.ERROR, "请先注册");
    }
    QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.SORT_NAME, sortName);
    queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    List<FileSort> fileSorts = fileSortService.list(queryWrapper);
    FileSort fileSort = null;
    if (fileSorts.size() >= 1) {
        fileSort = fileSorts.get(0);
    } else {
        return ResultUtil.result(SysConf.ERROR, "文件不被允许上传");
    }
    String sortUrl = fileSort.getUrl();
    // 判断url是否为空,如果为空,使用默认
    if (StringUtils.isEmpty(sortUrl)) {
        sortUrl = "base/common/";
    } else {
        sortUrl = fileSort.getUrl();
    }
    List<File> lists = new ArrayList<>();
    // 文件上传
    if (filedatas != null && filedatas.size() > 0) {
        for (MultipartFile filedata : filedatas) {
            String oldName = filedata.getOriginalFilename();
            long size = filedata.getSize();
            // 获取扩展名,默认是jpg
            String picExpandedName = FileUtils.getPicExpandedName(oldName);
            // 获取新文件名
            String newFileName = System.currentTimeMillis() + Constants.SYMBOL_POINT + picExpandedName;
            String localUrl = "";
            String qiNiuUrl = "";
            String minioUrl = "";
            try {
                MultipartFile tempFileData = filedata;
                // 上传七牛云,判断是否能够上传七牛云
                if (EOpenStatus.OPEN.equals(uploadQiNiu)) {
                    qiNiuUrl = qiniuService.uploadFile(tempFileData);
                }
                // 判断是否能够上传Minio文件服务器
                if (EOpenStatus.OPEN.equals(uploadMinio)) {
                    minioUrl = minioService.uploadFile(tempFileData);
                }
                // 判断是否能够上传至本地
                if (EOpenStatus.OPEN.equals(uploadLocal)) {
                    localUrl = localFileService.uploadFile(filedata, fileSort);
                }
            } catch (Exception e) {
                log.info("上传文件异常: {}", e.getMessage());
                e.getStackTrace();
                return ResultUtil.result(SysConf.ERROR, "文件上传失败,请检查系统配置");
            }
            File file = new File();
            file.setCreateTime(new Date(System.currentTimeMillis()));
            file.setFileSortUid(fileSort.getUid());
            file.setFileOldName(oldName);
            file.setFileSize(size);
            file.setPicExpandedName(picExpandedName);
            file.setPicName(newFileName);
            file.setPicUrl(localUrl);
            file.setStatus(EStatus.ENABLE);
            file.setUserUid(userUid);
            file.setAdminUid(adminUid);
            file.setQiNiuUrl(qiNiuUrl);
            file.setMinioUrl(minioUrl);
            file.insert();
            lists.add(file);
        }
        // 保存成功返回数据
        return ResultUtil.result(SysConf.SUCCESS, lists);
    }
    return ResultUtil.result(SysConf.ERROR, "请上传图片");
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) FileSort(com.moxi.mogublog.commons.entity.FileSort) File(com.moxi.mogublog.commons.entity.File) MultipartFile(org.springframework.web.multipart.MultipartFile) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException)

Example 4 with File

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

the class FileServiceImpl method ckeditorUploadCopyFile.

@Override
public Object ckeditorUploadCopyFile() {
    HttpServletRequest request = RequestHolder.getRequest();
    // 从参数中获取token【该方法用于ckeditor复制图片上传,所以会携带token在参数中】
    String token = request.getParameter(SysConf.TOKEN);
    if (StringUtils.isEmpty(token)) {
        throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "未读取到携带token");
    }
    String[] params = token.split("\\?url=");
    // 从Redis中获取系统配置文件
    Map<String, String> qiNiuConfig = new HashMap<>();
    Map<String, String> resultMap = feignUtil.getSystemConfigMap(params[0]);
    SystemConfig systemConfig = feignUtil.getSystemConfigByMap(resultMap);
    String userUid = "uid00000000000000000000000000000000";
    String adminUid = "uid00000000000000000000000000000000";
    String projectName = "blog";
    String sortName = "admin";
    // 需要上传的URL
    String itemUrl = params[1];
    // 判断需要上传的域名和本机图片域名是否一致
    if (EFilePriority.QI_NIU.equals(systemConfig.getContentPicturePriority())) {
        // 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
        if (StringUtils.isNotEmpty(systemConfig.getQiNiuPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getQiNiuPictureBaseUrl()) > -1) {
            Map<String, Object> result = new HashMap<>();
            result.put(SysConf.UPLOADED, 1);
            result.put(SysConf.FILE_NAME, itemUrl);
            result.put(SysConf.URL, itemUrl);
            return result;
        }
    } else if (EFilePriority.MINIO.equals(systemConfig.getContentPicturePriority())) {
        // 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
        if (StringUtils.isNotEmpty(systemConfig.getMinioPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getMinioPictureBaseUrl()) > -1) {
            Map<String, Object> result = new HashMap<>();
            result.put(SysConf.UPLOADED, 1);
            result.put(SysConf.FILE_NAME, itemUrl);
            result.put(SysConf.URL, itemUrl);
            return result;
        }
    } else {
        // 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
        if (StringUtils.isNotEmpty(systemConfig.getLocalPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getLocalPictureBaseUrl()) > -1) {
            Map<String, Object> result = new HashMap<>();
            result.put(SysConf.UPLOADED, 1);
            result.put(SysConf.FILE_NAME, itemUrl);
            result.put(SysConf.URL, itemUrl);
            return result;
        }
    }
    // projectName现在默认base
    if (StringUtils.isEmpty(projectName)) {
        projectName = "base";
    }
    // TODO 这里可以检测用户上传,如果不是网站的用户或会员就不能调用
    if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
        return ResultUtil.result(SysConf.ERROR, "请先注册");
    }
    QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.SORT_NAME, sortName);
    queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    List<FileSort> fileSorts = fileSortService.list(queryWrapper);
    FileSort fileSort = null;
    if (fileSorts.size() > 0) {
        fileSort = fileSorts.get(0);
    } else {
        return ResultUtil.result(SysConf.ERROR, "文件不被允许上传");
    }
    String sortUrl = fileSort.getUrl();
    // 判断url是否为空,如果为空,使用默认
    if (StringUtils.isEmpty(sortUrl)) {
        sortUrl = "base/common/";
    } else {
        sortUrl = fileSort.getUrl();
    }
    // 获取新文件名(默认为jpg)
    String newFileName = System.currentTimeMillis() + ".jpg";
    // 文件url访问地址
    String localUrl = "";
    String qiNiuUrl = "";
    String minioUrl = "";
    // 上传到本地服务器【判断是否能够上传至本地】
    if (EOpenStatus.OPEN.equals(systemConfig.getUploadLocal())) {
        localUrl = localFileService.uploadPictureByUrl(itemUrl, fileSort);
    }
    // 上传七牛云 【判断是否能够上传七牛云】
    if (EOpenStatus.OPEN.equals(systemConfig.getUploadMinio())) {
        minioUrl = minioService.uploadPictureByUrl(itemUrl);
    }
    // 上传七牛云 【判断是否能够上传七牛云】
    if (EOpenStatus.OPEN.equals(systemConfig.getUploadQiNiu())) {
        qiNiuUrl = qiniuService.uploadPictureByUrl(itemUrl, systemConfig);
    }
    File file = new File();
    file.setCreateTime(new Date(System.currentTimeMillis()));
    file.setFileSortUid(fileSort.getUid());
    file.setFileOldName(itemUrl);
    file.setFileSize(0L);
    file.setPicExpandedName(Constants.FILE_SUFFIX_JPG);
    file.setPicName(newFileName);
    // 设置本地图片
    file.setPicUrl(systemConfig.getLocalPictureBaseUrl() + localUrl);
    // 设置minio图片
    file.setMinioUrl(systemConfig.getMinioPictureBaseUrl() + minioUrl);
    // 设置七牛云图片
    file.setQiNiuUrl(systemConfig.getQiNiuPictureBaseUrl() + qiNiuUrl);
    file.setStatus(EStatus.ENABLE);
    file.setUserUid(userUid);
    file.setAdminUid(adminUid);
    fileService.save(file);
    Map<String, Object> result = new HashMap<>();
    result.put(SysConf.UPLOADED, 1);
    result.put(SysConf.FILE_NAME, newFileName);
    // 设置显示方式
    if (EFilePriority.QI_NIU.equals(qiNiuConfig.get(SysConf.PICTURE_PRIORITY))) {
        result.put(SysConf.URL, systemConfig.getQiNiuPictureBaseUrl() + qiNiuUrl);
    } else if (EFilePriority.MINIO.equals(qiNiuConfig.get(SysConf.PICTURE_PRIORITY))) {
        result.put(SysConf.URL, systemConfig.getMinioPictureBaseUrl() + localUrl);
    } else {
        result.put(SysConf.URL, systemConfig.getLocalPictureBaseUrl() + localUrl);
    }
    return result;
}
Also used : SystemConfig(com.moxi.mogublog.commons.entity.SystemConfig) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) FileSort(com.moxi.mogublog.commons.entity.FileSort) File(com.moxi.mogublog.commons.entity.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 5 with File

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

the class FileServiceImpl method uploadPictureByUrl.

@Override
public String uploadPictureByUrl(FileVO fileVO) {
    // 获取配置文件
    SystemConfig systemConfig;
    if (fileVO.getSystemConfig() != null) {
        Map<String, String> resultMap = fileVO.getSystemConfig();
        systemConfig = feignUtil.getSystemConfigByMap(resultMap);
    } else {
        // 从Redis中获取七牛云配置文件
        systemConfig = feignUtil.getSystemConfig();
    }
    String userUid = fileVO.getUserUid();
    String adminUid = fileVO.getAdminUid();
    String projectName = fileVO.getProjectName();
    String sortName = fileVO.getSortName();
    List<String> urlList = fileVO.getUrlList();
    // projectName现在默认base
    if (StringUtils.isEmpty(projectName)) {
        projectName = "base";
    }
    // 这里可以检测用户上传,如果不是网站的用户或会员就不能调用
    if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
        throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "请先注册");
    }
    QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.SORT_NAME, sortName);
    queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    List<FileSort> fileSorts = fileSortService.list(queryWrapper);
    FileSort fileSort = null;
    if (fileSorts.size() > 0) {
        fileSort = fileSorts.get(0);
    } else {
        throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "文件不被允许上传, 请填写文件分类信息");
    }
    List<com.moxi.mogublog.commons.entity.File> lists = new ArrayList<>();
    // 文件上传
    if (urlList != null && urlList.size() > 0) {
        for (String itemUrl : urlList) {
            // 获取新文件名(默认为jpg)
            String newFileName = System.currentTimeMillis() + ".jpg";
            // 将图片上传到本地服务器中以及七牛云中
            String picurl = "";
            String qiNiuUrl = "";
            String minioUrl = "";
            // 判断是否能够上传至本地
            if (EOpenStatus.OPEN.equals(systemConfig.getUploadLocal())) {
                picurl = localFileService.uploadPictureByUrl(itemUrl, fileSort);
            }
            // 上传七牛云,判断是否能够上传七牛云
            if (EOpenStatus.OPEN.equals(systemConfig.getUploadMinio())) {
                minioUrl = minioService.uploadPictureByUrl(itemUrl);
            }
            // 上传七牛云,判断是否能够上传七牛云
            if (EOpenStatus.OPEN.equals(systemConfig.getUploadQiNiu())) {
                qiNiuUrl = qiniuService.uploadPictureByUrl(itemUrl, systemConfig);
            }
            com.moxi.mogublog.commons.entity.File file = new com.moxi.mogublog.commons.entity.File();
            file.setCreateTime(new Date(System.currentTimeMillis()));
            file.setFileSortUid(fileSort.getUid());
            file.setFileOldName(itemUrl);
            file.setFileSize(0L);
            file.setPicExpandedName("jpg");
            file.setPicName(newFileName);
            file.setPicUrl(picurl);
            file.setQiNiuUrl(qiNiuUrl);
            file.setMinioUrl(minioUrl);
            file.setStatus(EStatus.ENABLE);
            file.setUserUid(userUid);
            file.setAdminUid(adminUid);
            fileService.save(file);
            lists.add(file);
        }
        // 保存成功返回数据
        return ResultUtil.result(SysConf.SUCCESS, lists);
    }
    return ResultUtil.result(SysConf.ERROR, "请上传图片");
}
Also used : SystemConfig(com.moxi.mogublog.commons.entity.SystemConfig) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) File(com.moxi.mogublog.commons.entity.File) FileSort(com.moxi.mogublog.commons.entity.FileSort) File(com.moxi.mogublog.commons.entity.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

File (com.moxi.mogublog.commons.entity.File)5 MultipartFile (org.springframework.web.multipart.MultipartFile)5 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 FileSort (com.moxi.mogublog.commons.entity.FileSort)3 SystemConfig (com.moxi.mogublog.commons.entity.SystemConfig)3 InsertException (com.moxi.mougblog.base.exception.exceptionType.InsertException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 NetworkDisk (com.moxi.mogublog.commons.entity.NetworkDisk)1 Storage (com.moxi.mogublog.commons.entity.Storage)1 QueryException (com.moxi.mougblog.base.exception.exceptionType.QueryException)1 UpdateException (com.moxi.mougblog.base.exception.exceptionType.UpdateException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)1