1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.landtool.lanbase.modules.sys.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.landtool.lanbase.config.SysTemPropertyConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import com.landtool.lanbase.common.annotation.LogAction;
import com.landtool.lanbase.common.annotation.SysLog;
import com.landtool.lanbase.common.exception.LanbaseException;
import com.landtool.lanbase.common.utils.*;
import com.landtool.lanbase.modules.sys.entity.SysAttachment;
import com.landtool.lanbase.modules.sys.service.SysAttachmentService;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author lanbase
 * @Description: TODO(附件)
 * @date 2017-7-10 17:07
 */
@Controller
@RequestMapping("/sys/attachment")
@Api(value = "", tags = {"系统附件"})
public class SysAttachmentController extends AbstractController {
 
    @Autowired
    private SysAttachmentService sysAttachmentService;
 
    @Autowired
    private SysTemPropertyConfig sysProps;
 
    /**
     * 所有附件列表
     */
    @RequestMapping("/list")
 //   @RequiresPermissions("sys:attachment:list")
    @ResponseBody
    @ApiOperation(
            value = "附件列表",
            notes = "所有附件列表"
    )
//    @LogAction("查询")
    public Result list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<SysAttachment> configList=sysAttachmentService.queryList(query);
        int total=sysAttachmentService.queryTotal(query);
        PageUtils pageUtil=new PageUtils(configList, total, query.getLimit(), query.getPage());
        return Result.ok().put("page", pageUtil);
    }
 
    /**
     * 附件信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
 //   @RequiresPermissions("sys:attachment:list")
    @ApiOperation(
            value = "附件信息",
            notes = ""
    )
    public Result info(@ApiParam(name="id",value="文件id",required=true)
                       @PathVariable("id") Long id){
        SysAttachment attachment = sysAttachmentService.queryObject(id);
        return Result.ok().put("attachment", attachment);
    }
 
    /**
     * 上传文件
     */
    @RequestMapping("/upload")
 //   @RequiresPermissions("sys:attachment:edit")
    @ApiOperation(
            value = "上传文件",
            notes = ""
    )
    public String upload(HttpServletRequest request) {
        String callBackUrl = request.getParameter("callBackUrl");
        String category = request.getParameter("category");
        String whereStore = "";
        try {
            String root = sysProps.getUploadPath().replace("/","\\");
            String LinShiUrl=root+"\\temp";
            List<MultipartFile> files=((MultipartHttpServletRequest) request).getFiles("file");
            if(files.isEmpty()){
                throw new LanbaseException("上传文件不能为空");
            }
            for(MultipartFile file:files){
                String suffix=FileUtils.getSuffix(file.getOriginalFilename());
                String newFileName=AttachmentUtils.newFileName(suffix,LinShiUrl,category);
                File newFile=new File(newFileName);
                //临时文件处理
                file.transferTo(newFile);
                String path=FileUtils.removePrefix(newFile.getAbsolutePath(),root).replace("\\", "/");
                SysAttachment sysAttachment=new SysAttachment();
                sysAttachment.setUserId(getUserId());
                sysAttachment.setTitle(file.getOriginalFilename());
                sysAttachment.setPath(path);
                sysAttachment.setSuffix(suffix);
                sysAttachment.setMimeType(file.getContentType());
                sysAttachment.setCreateTime(new Date());
                sysAttachmentService.save(sysAttachment);
                whereStore = sysAttachment.getPath();
            }
            System.out.println("redirect:" + callBackUrl + "?code=0&textpath=" + whereStore);
            return "redirect:" + callBackUrl + "?code=0&textpath=" + whereStore;
        } catch (IOException e) {
            throw new LanbaseException("系统异常");
        }
    }
 
    /**
     * 下载文件
     */
    @RequestMapping("/download/{id}")
    @ResponseBody
 //   @RequiresPermissions("sys:attachment:list")
    @ApiOperation(
            value = "下载文件",
            notes = ""
    )
    public void download(@ApiParam(name="id",value="文件id",required=true)
                         @PathVariable("id") Long id, 
                         HttpServletResponse response) {
        SysAttachment attachment=sysAttachmentService.queryObject(id);
        if(attachment==null){
            throw new LanbaseException("文件不存在");
        }
 
        String path=sysProps.getUploadPath()+attachment.getPath();
        FileUtils.download(path, attachment.getTitle(), response);
    }
 
    /**
     * 删除配置
     */
    @SysLog("删除配置")
    @RequestMapping("/delete")
    @ResponseBody
 //   @RequiresPermissions("sys:attachment:edit")
    @ApiOperation(
            value = "删除配置",
            notes = ""
    )
//    @LogAction("删除")
    public Result delete(@RequestBody Long[] ids){
        sysAttachmentService.deleteBatch(ids);
        return Result.ok();
    }
 
}