Search in sources :

Example 1 with Attachment

use of cc.ryanc.halo.model.domain.Attachment in project halo by ruibaby.

the class AttachmentController method attachments.

/**
 * 获取upload的所有图片资源并渲染页面
 *
 * @param model model
 * @return String
 */
@GetMapping
public String attachments(Model model, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "18") Integer size) {
    Sort sort = new Sort(Sort.Direction.DESC, "attachId");
    Pageable pageable = new PageRequest(page, size, sort);
    Page<Attachment> attachments = attachmentService.findAllAttachments(pageable);
    model.addAttribute("attachments", attachments);
    // 设置选项
    model.addAttribute("options", HaloConst.OPTIONS);
    return "admin/admin_attachment";
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Attachment(cc.ryanc.halo.model.domain.Attachment)

Example 2 with Attachment

use of cc.ryanc.halo.model.domain.Attachment in project halo by ruibaby.

the class AttachmentController method removeAttachment.

/**
 * 移除附件的请求
 *
 * @param attachId attachId
 * @return string
 */
@GetMapping(value = "/remove")
@ResponseBody
public boolean removeAttachment(@PathParam("attachId") Long attachId, HttpServletRequest request) {
    Optional<Attachment> attachment = attachmentService.findByAttachId(attachId);
    String delFileName = attachment.get().getAttachName();
    String delSmallFileName = delFileName.substring(0, delFileName.lastIndexOf('.')) + "_small" + attachment.get().getAttachSuffix();
    try {
        // 删除数据库中的内容
        attachmentService.removeByAttachId(attachId);
        // 刷新HaloConst变量
        updateConst();
        // 删除文件
        File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
        File mediaPath = new File(basePath.getAbsolutePath(), attachment.get().getAttachPath().substring(0, attachment.get().getAttachPath().lastIndexOf('/')));
        File delFile = new File(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(delFileName).toString());
        File delSmallFile = new File(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(delSmallFileName).toString());
        BufferedImage sourceImg = ImageIO.read(new FileInputStream(delFile));
        if (sourceImg.getWidth() > 500 && sourceImg.getHeight() > 500) {
            if (delSmallFile.exists()) {
                if (delSmallFile.delete()) {
                    updateConst();
                }
            }
        }
        if (delFile.exists() && delFile.isFile()) {
            if (delFile.delete()) {
                updateConst();
                log.info("删除文件[" + delFileName + "]成功!");
                logsService.saveByLogs(new Logs(LogsRecord.REMOVE_FILE, delFileName, HaloUtil.getIpAddr(request), HaloUtil.getDate()));
            } else {
                log.error("删除附件[" + delFileName + "]失败!");
                return false;
            }
        }
    } catch (Exception e) {
        log.error("删除附件[" + delFileName + "]失败!" + e.getMessage());
        return false;
    }
    return true;
}
Also used : Attachment(cc.ryanc.halo.model.domain.Attachment) Logs(cc.ryanc.halo.model.domain.Logs) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 3 with Attachment

use of cc.ryanc.halo.model.domain.Attachment in project halo by ruibaby.

the class AttachmentController method uploadAttachment.

/**
 * 上传文件
 *
 * @param file file
 */
@PostMapping(value = "/upload", produces = { "application/json;charset=UTF-8" })
@ResponseBody
public boolean uploadAttachment(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    if (!file.isEmpty()) {
        try {
            File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
            StringBuffer sbMedia = new StringBuffer("upload/");
            sbMedia.append(HaloUtil.YEAR).append("/").append(HaloUtil.MONTH).append("/");
            File mediaPath = new File(basePath.getAbsolutePath(), sbMedia.toString());
            if (!mediaPath.exists()) {
                mediaPath.mkdirs();
            }
            file.transferTo(new File(mediaPath.getAbsoluteFile(), file.getOriginalFilename()));
            String fileName = file.getOriginalFilename();
            String nameWithOutSuffix = fileName.substring(0, fileName.lastIndexOf('.'));
            String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.') + 1);
            // 保存在数据库
            Attachment attachment = new Attachment();
            attachment.setAttachName(fileName);
            attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtil.YEAR).append("/").append(HaloUtil.MONTH).append("/").append(fileName).toString());
            System.out.println(mediaPath.getAbsolutePath() + "/" + fileName);
            // 判断图片大小,如果长宽都小于500,则保存原始图片路径
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(mediaPath.getPath() + "/" + fileName));
            if (sourceImg.getWidth() < 500 && sourceImg.getHeight() < 500) {
                attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtil.YEAR).append("/").append(HaloUtil.MONTH).append("/").append(fileName).toString());
            } else {
                attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtil.YEAR).append("/").append(HaloUtil.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
                // 剪裁图片
                HaloUtil.cutCenterImage(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(fileName).toString(), new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString(), 500, 500, fileSuffix);
            }
            attachment.setAttachType(file.getContentType());
            attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
            attachment.setAttachCreated(HaloUtil.getDate());
            attachmentService.saveByAttachment(attachment);
            updateConst();
            log.info("上传文件[" + file.getOriginalFilename() + "]到[" + mediaPath.getAbsolutePath() + "]成功");
            logsService.saveByLogs(new Logs(LogsRecord.UPLOAD_FILE, file.getOriginalFilename(), HaloUtil.getIpAddr(request), HaloUtil.getDate()));
            return true;
        } catch (Exception e) {
            log.error("未知错误:" + e.getMessage());
        }
    } else {
        log.error("文件不能为空");
    }
    return false;
}
Also used : Attachment(cc.ryanc.halo.model.domain.Attachment) Logs(cc.ryanc.halo.model.domain.Logs) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 4 with Attachment

use of cc.ryanc.halo.model.domain.Attachment in project halo by ruibaby.

the class AttachmentController method selectAttachment.

/**
 * 跳转选择附件页面
 *
 * @param model model
 * @param page page
 * @return string
 */
@GetMapping(value = "/select")
public String selectAttachment(Model model, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "id") String id) {
    Sort sort = new Sort(Sort.Direction.DESC, "attachId");
    Pageable pageable = new PageRequest(page, 18, sort);
    Page<Attachment> attachments = attachmentService.findAllAttachments(pageable);
    model.addAttribute("attachments", attachments);
    model.addAttribute("id", id);
    // 设置选项
    model.addAttribute("options", HaloConst.OPTIONS);
    return "admin/widget/_attachment-select";
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Attachment(cc.ryanc.halo.model.domain.Attachment)

Aggregations

Attachment (cc.ryanc.halo.model.domain.Attachment)4 Logs (cc.ryanc.halo.model.domain.Logs)2 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 PageRequest (org.springframework.data.domain.PageRequest)2 Pageable (org.springframework.data.domain.Pageable)2 Sort (org.springframework.data.domain.Sort)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2