Search in sources :

Example 1 with InsertException

use of com.moxi.mougblog.base.exception.exceptionType.InsertException in project mogu_blog_v2 by moxi624.

the class UserServiceImpl method insertUserInfo.

@Override
public User insertUserInfo(HttpServletRequest request, String response) {
    Map<String, Object> map = JsonUtils.jsonToMap(response);
    boolean exist = false;
    User user = new User();
    Map<String, Object> data = JsonUtils.jsonToMap(JsonUtils.objectToJson(map.get(SysConf.DATA)));
    if (data.get(SysConf.UUID) != null && data.get(SysConf.SOURCE) != null) {
        if (getUserBySourceAnduuid(data.get(SysConf.SOURCE).toString(), data.get(SysConf.UUID).toString()) != null) {
            user = getUserBySourceAnduuid(data.get(SysConf.SOURCE).toString(), data.get(SysConf.UUID).toString());
            exist = true;
        }
    } else {
        log.error("未获取到uuid或source");
        throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, MessageConf.INSERT_DEFAULT_ERROR);
    }
    if (data.get(SysConf.EMAIL) != null) {
        user.setEmail(data.get(SysConf.EMAIL).toString());
    }
    if (data.get(SysConf.AVATAR) != null) {
        user.setAvatar(data.get(SysConf.AVATAR).toString());
    }
    if (data.get(SysConf.NICKNAME) != null) {
        user.setNickName(data.get(SysConf.NICKNAME).toString());
    }
    user.setLoginCount(user.getLoginCount() + 1);
    user.setLastLoginTime(new Date());
    user.setLastLoginIp(IpUtils.getIpAddr(request));
    if (exist) {
        user.updateById();
    } else {
        user.setUuid(data.get(SysConf.UUID).toString());
        user.setSource(data.get(SysConf.SOURCE).toString());
        user.setUserName("mg".concat(user.getSource()).concat(user.getUuid()));
        // 产生(0,999999]之间的随机数
        Integer randNum = (int) (Math.random() * (999999) + 1);
        // 进行六位数补全
        String workPassWord = String.format("%06d", randNum);
        user.setPassWord(workPassWord);
        user.insert();
    }
    return user;
}
Also used : User(com.moxi.mogublog.commons.entity.User) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException)

Example 2 with InsertException

use of com.moxi.mougblog.base.exception.exceptionType.InsertException in project mogu_blog_v2 by moxi624.

the class LocalFileServiceImpl method uploadPictureByUrl.

@Override
public String uploadPictureByUrl(String itemUrl, FileSort fileSort) {
    String sortUrl = fileSort.getUrl();
    // 判断url是否为空,如果为空,使用默认
    if (StringUtils.isEmpty(sortUrl)) {
        sortUrl = "base/common/";
    } else {
        sortUrl = fileSort.getUrl();
    }
    // 获取新文件名 【默认为jpg】
    String newFileName = System.currentTimeMillis() + ".jpg";
    // 文件绝对路径
    String newPath = path + sortUrl + "/jpg/" + DateUtils.getYears() + "/" + DateUtils.getMonth() + "/" + DateUtils.getDay() + "/";
    // 文件相对路径
    String fileUrl = sortUrl + "/jpg/" + DateUtils.getYears() + "/" + DateUtils.getMonth() + "/" + DateUtils.getDay() + "/" + newFileName;
    String saveUrl = newPath + newFileName;
    // 将图片上传到本地服务器中以及七牛云中
    BufferedOutputStream out = null;
    FileOutputStream os = null;
    // 输入流
    InputStream inputStream = null;
    // 判断文件是否存在
    java.io.File file1 = new java.io.File(newPath);
    if (!file1.exists()) {
        file1.mkdirs();
    }
    try {
        // 构造URL
        URL url = new URL(itemUrl);
        // 打开连接
        URLConnection con = url.openConnection();
        // 设置用户代理
        con.setRequestProperty("User-agent", "	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
        // 设置10秒
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        // 当获取的相片无法正常显示的时候,需要给一个默认图片
        inputStream = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        java.io.File file = new java.io.File(saveUrl);
        os = new FileOutputStream(file, true);
        // 开始读取
        while ((len = inputStream.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        return fileUrl;
    } catch (Exception e) {
        log.error("上传图片失败: {}", e.getMessage());
        throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "获取图片超时,文件上传失败");
    } finally {
        try {
            // 完毕,关闭所有链接
            os.close();
            inputStream.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
Also used : InputStream(java.io.InputStream) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) URL(java.net.URL) URLConnection(java.net.URLConnection) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 3 with InsertException

use of com.moxi.mougblog.base.exception.exceptionType.InsertException in project mogu_blog_v2 by moxi624.

the class MinioServiceImpl method uploadPictureByUrl.

@Override
public String uploadPictureByUrl(String itemUrl) {
    java.io.File dest = null;
    // 将图片上传到本地服务器中以及七牛云中
    BufferedOutputStream out = null;
    FileOutputStream os = null;
    // 输入流
    InputStream inputStream = null;
    // 获取新文件名 【默认为jpg】
    String newFileName = System.currentTimeMillis() + ".jpg";
    try {
        // 构造URL
        URL url = new URL(itemUrl);
        // 打开连接
        URLConnection con = url.openConnection();
        // 设置用户代理
        con.setRequestProperty("User-agent", "	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
        // 设置10秒
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        // 当获取的相片无法正常显示的时候,需要给一个默认图片
        inputStream = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        String tempFiles = "temp/" + newFileName;
        dest = new java.io.File(tempFiles);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        os = new FileOutputStream(dest, true);
        // 开始读取
        while ((len = inputStream.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        FileInputStream fileInputStream = new FileInputStream(dest);
        MultipartFile fileData = new MockMultipartFile(dest.getName(), dest.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        out = new BufferedOutputStream(new FileOutputStream(dest));
        out.write(fileData.getBytes());
        // TODO 不关闭流,小图片就无法显示?
        out.flush();
        out.close();
        MultipartFile multipartFile = MoGuFileUtil.fileToMultipartFile(dest);
        return minioUtil.uploadFile(multipartFile);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new InsertException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
    } finally {
        if (dest != null && dest.getParentFile().exists()) {
            dest.delete();
        }
    }
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) URL(java.net.URL) URLConnection(java.net.URLConnection) FileInputStream(java.io.FileInputStream) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with InsertException

use of com.moxi.mougblog.base.exception.exceptionType.InsertException in project mogu_blog_v2 by moxi624.

the class QiniuServiceImpl method uploadPictureByUrl.

@Override
public String uploadPictureByUrl(String itemUrl, SystemConfig systemConfig) {
    java.io.File dest = null;
    // 将图片上传到本地服务器中以及七牛云中
    BufferedOutputStream out = null;
    FileOutputStream os = null;
    // 输入流
    InputStream inputStream = null;
    // 获取新文件名 【默认为jpg】
    String newFileName = System.currentTimeMillis() + ".jpg";
    try {
        // 构造URL
        URL url = new URL(itemUrl);
        // 打开连接
        URLConnection con = url.openConnection();
        // 设置用户代理
        con.setRequestProperty("User-agent", "	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
        // 设置10秒
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        // 当获取的相片无法正常显示的时候,需要给一个默认图片
        inputStream = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        String tempFiles = "temp/" + newFileName;
        dest = new java.io.File(tempFiles);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        os = new FileOutputStream(dest, true);
        // 开始读取
        while ((len = inputStream.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        FileInputStream fileInputStream = new FileInputStream(dest);
        MultipartFile fileData = new MockMultipartFile(dest.getName(), dest.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        out = new BufferedOutputStream(new FileOutputStream(dest));
        out.write(fileData.getBytes());
        QiniuUtil qn = new QiniuUtil();
        // TODO 不关闭流,小图片就无法显示?
        out.flush();
        out.close();
        return qn.uploadQiniu(dest, systemConfig);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new InsertException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
    } finally {
        if (dest != null && dest.getParentFile().exists()) {
            dest.delete();
        }
    }
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) java.io(java.io) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) URL(java.net.URL) URLConnection(java.net.URLConnection) InsertException(com.moxi.mougblog.base.exception.exceptionType.InsertException) QiniuUtil(com.moxi.mogublog.picture.util.QiniuUtil) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 5 with InsertException

use of com.moxi.mougblog.base.exception.exceptionType.InsertException 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)

Aggregations

InsertException (com.moxi.mougblog.base.exception.exceptionType.InsertException)6 MultipartFile (org.springframework.web.multipart.MultipartFile)5 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 File (com.moxi.mogublog.commons.entity.File)2 FileSort (com.moxi.mogublog.commons.entity.FileSort)2 SystemConfig (com.moxi.mogublog.commons.entity.SystemConfig)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 User (com.moxi.mogublog.commons.entity.User)1 QiniuUtil (com.moxi.mogublog.picture.util.QiniuUtil)1 java.io (java.io)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)1