Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 212d392

Browse files
committed
完成文件管理部分
1 parent e684273 commit 212d392

10 files changed

Lines changed: 395 additions & 18 deletions

File tree

src/main/java/com/zhazhapan/efo/dao/FileDAO.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.zhazhapan.efo.dao.sqlprovider.FileSqlProvider;
44
import com.zhazhapan.efo.entity.File;
5+
import com.zhazhapan.efo.model.BaseAuthRecord;
56
import com.zhazhapan.efo.model.FileBasicRecord;
67
import com.zhazhapan.efo.model.FileRecord;
78
import org.apache.ibatis.annotations.*;
@@ -16,6 +17,16 @@
1617
@Repository
1718
public interface FileDAO {
1819

20+
/**
21+
* 获取文件权限
22+
*
23+
* @param id 文件编号
24+
*
25+
* @return {@link BaseAuthRecord}
26+
*/
27+
@Select("select is_downloadable,is_uploadable,is_deletable,is_updatable,is_visible from file where id=#{id}")
28+
BaseAuthRecord getAuth(long id);
29+
1930
/**
2031
* 通过编号获取文件
2132
*
@@ -174,11 +185,13 @@ public interface FileDAO {
174185
* @param isVisible 可查权限
175186
* @param isDeletable 删除权限
176187
* @param isUpdatable 上传权限
188+
*
189+
* @return 是否更新成功
177190
*/
178191
@UpdateProvider(type = FileSqlProvider.class, method = "updateAuthById")
179-
void updateAuthById(@Param("id") int id, @Param("isDownloadable") int isDownloadable, @Param("isUploadable") int
180-
isUploadable, @Param("isVisible") int isVisible, @Param("isDeletable") int isDeletable, @Param
181-
("isUpdatable") int isUpdatable);
192+
boolean updateAuthById(@Param("id") long id, @Param("isDownloadable") int isDownloadable, @Param("isUploadable")
193+
int isUploadable, @Param("isDeletable") int isDeletable, @Param("isUpdatable") int isUpdatable, @Param
194+
("isVisible") int isVisible);
182195

183196
/**
184197
* 更新文件名
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.zhazhapan.efo.model;
2+
3+
/**
4+
* @author pantao
5+
* @since 2018/3/6
6+
*/
7+
public class BaseAuthRecord {
8+
9+
private int isDownloadable;
10+
11+
private int isUploadable;
12+
13+
private int isDeletable;
14+
15+
private int isUpdatable;
16+
17+
private int isVisible;
18+
19+
public BaseAuthRecord(int isDownloadable, int isUploadable, int isDeletable, int isUpdatable, int isVisible) {
20+
this.isDownloadable = isDownloadable;
21+
this.isUploadable = isUploadable;
22+
this.isDeletable = isDeletable;
23+
this.isUpdatable = isUpdatable;
24+
this.isVisible = isVisible;
25+
}
26+
27+
public int getIsDownloadable() {
28+
return isDownloadable;
29+
}
30+
31+
public void setIsDownloadable(int isDownloadable) {
32+
this.isDownloadable = isDownloadable;
33+
}
34+
35+
public int getIsUploadable() {
36+
return isUploadable;
37+
}
38+
39+
public void setIsUploadable(int isUploadable) {
40+
this.isUploadable = isUploadable;
41+
}
42+
43+
public int getIsDeletable() {
44+
return isDeletable;
45+
}
46+
47+
public void setIsDeletable(int isDeletable) {
48+
this.isDeletable = isDeletable;
49+
}
50+
51+
public int getIsUpdatable() {
52+
return isUpdatable;
53+
}
54+
55+
public void setIsUpdatable(int isUpdatable) {
56+
this.isUpdatable = isUpdatable;
57+
}
58+
59+
public int getIsVisible() {
60+
return isVisible;
61+
}
62+
63+
public void setIsVisible(int isVisible) {
64+
this.isVisible = isVisible;
65+
}
66+
}

src/main/java/com/zhazhapan/efo/service/IFileService.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.zhazhapan.efo.service;
22

33
import com.zhazhapan.efo.entity.User;
4+
import com.zhazhapan.efo.model.BaseAuthRecord;
45
import com.zhazhapan.efo.model.FileBasicRecord;
56
import com.zhazhapan.efo.model.FileRecord;
67
import org.springframework.web.multipart.MultipartFile;
@@ -15,6 +16,46 @@
1516
*/
1617
public interface IFileService {
1718

19+
/**
20+
* 更新文件权限
21+
*
22+
* @param id 文件编号
23+
* @param auth 权限集
24+
*
25+
* @return 是否更新成功
26+
*/
27+
boolean updateAuth(long id, String auth);
28+
29+
/**
30+
* 获取文件权限
31+
*
32+
* @param id 文件编号
33+
*
34+
* @return {@link BaseAuthRecord}
35+
*/
36+
BaseAuthRecord getAuth(long id);
37+
38+
/**
39+
* 批量删除文件
40+
*
41+
* @param ids 所有文件编号
42+
*
43+
* @return 是否删除成功
44+
*/
45+
boolean deleteFiles(String ids);
46+
47+
/**
48+
* 更新文件路径
49+
*
50+
* @param id 文件编号
51+
* @param oldLocalUrl 原本地路径
52+
* @param localUrl 本地路径
53+
* @param visitUrl 访问路径
54+
*
55+
* @return 是否更新成功
56+
*/
57+
boolean[] updateUrl(int id, String oldLocalUrl, String localUrl, String visitUrl);
58+
1859
/**
1960
* 更新文件信息
2061
*

src/main/java/com/zhazhapan/efo/service/impl/FileServiceImpl.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import com.zhazhapan.efo.entity.File;
99
import com.zhazhapan.efo.entity.User;
1010
import com.zhazhapan.efo.model.AuthRecord;
11+
import com.zhazhapan.efo.model.BaseAuthRecord;
1112
import com.zhazhapan.efo.model.FileBasicRecord;
1213
import com.zhazhapan.efo.model.FileRecord;
1314
import com.zhazhapan.efo.modules.constant.ConfigConsts;
1415
import com.zhazhapan.efo.modules.constant.DefaultValues;
1516
import com.zhazhapan.efo.service.IAuthService;
1617
import com.zhazhapan.efo.service.ICategoryService;
1718
import com.zhazhapan.efo.service.IFileService;
19+
import com.zhazhapan.efo.util.BeanUtils;
1820
import com.zhazhapan.efo.util.ServiceUtils;
1921
import com.zhazhapan.modules.constant.ValueConsts;
2022
import com.zhazhapan.util.*;
@@ -79,6 +81,51 @@ public FileServiceImpl(FileDAO fileDAO, ICategoryService categoryService, IAuthS
7981
this.downloadDAO = downloadDAO;
8082
}
8183

84+
@Override
85+
public boolean updateAuth(long id, String auth) {
86+
int[] au = BeanUtils.getAuth(auth);
87+
return fileDAO.updateAuthById(id, au[0], au[1], au[2], au[3], au[4]);
88+
}
89+
90+
@Override
91+
public BaseAuthRecord getAuth(long id) {
92+
return fileDAO.getAuth(id);
93+
}
94+
95+
@Override
96+
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
97+
Exception.class)
98+
public boolean deleteFiles(String ids) {
99+
if (Checker.isNotEmpty(ids)) {
100+
String[] id = ids.split(ValueConsts.COMMA_SIGN);
101+
for (String s : id) {
102+
long fileId = Formatter.stringToLong(s);
103+
String localUrl = fileDAO.getLocalUrlById(fileId);
104+
FileExecutor.deleteFile(localUrl);
105+
removeById(fileId);
106+
}
107+
return true;
108+
}
109+
return false;
110+
}
111+
112+
@Override
113+
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
114+
Exception.class)
115+
public boolean[] updateUrl(int id, String oldLocalUrl, String localUrl, String visitUrl) {
116+
boolean[] b = new boolean[]{false, false};
117+
if (Checker.isNotEmpty(localUrl) && Checker.isNotExists(localUrl) && !localUrlExists(localUrl)) {
118+
FileExecutor.renameTo(oldLocalUrl, localUrl);
119+
fileDAO.updateLocalUrlById(id, localUrl);
120+
b[0] = true;
121+
}
122+
if (Checker.isNotEmpty(visitUrl) && !visitUrlExists(visitUrl)) {
123+
fileDAO.updateVisitUrlById(id, visitUrl);
124+
b[1] = true;
125+
}
126+
return b;
127+
}
128+
82129
@Override
83130
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
84131
Exception.class)

src/main/java/com/zhazhapan/efo/util/BeanUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.zhazhapan.efo.util;
22

33
import com.alibaba.fastjson.JSONObject;
4+
import com.zhazhapan.modules.constant.ValueConsts;
45
import com.zhazhapan.util.Formatter;
56
import com.zhazhapan.util.enums.FieldModifier;
67
import org.slf4j.Logger;
@@ -20,6 +21,23 @@ public class BeanUtils {
2021

2122
private BeanUtils() {}
2223

24+
/**
25+
* 将权限字符串装换成权限数组
26+
*
27+
* @param auth 权限字符串
28+
*
29+
* @return 权限数组
30+
*/
31+
public static int[] getAuth(String auth) {
32+
int[] a = new int[5];
33+
String[] u = auth.split(ValueConsts.COMMA_SIGN);
34+
int len = Math.min(a.length, u.length);
35+
for (int i = 0; i < len; i++) {
36+
a[i] = Formatter.stringToInt(u[i]);
37+
}
38+
return a;
39+
}
40+
2341
/**
2442
* 将Bean转换成JSON
2543
*

src/main/java/com/zhazhapan/efo/web/controller/FileController.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String getBasicAll(String user, String file, String category, int offset)
118118
return Formatter.listToJson(fileService.getBasicAll(user, file, category, offset));
119119
}
120120

121-
@AuthInterceptor
121+
@AuthInterceptor(InterceptorLevel.ADMIN)
122122
@RequestMapping(value = "/server", method = RequestMethod.GET)
123123
public String getServerFilesByPath(String path) {
124124
File[] files = FileExecutor.listFile(Checker.isEmpty(path) ? (Checker.isWindows() ? "C:\\" : "/") : path);
@@ -131,13 +131,39 @@ public String getServerFilesByPath(String path) {
131131
return array.toJSONString();
132132
}
133133

134-
@AuthInterceptor
134+
@AuthInterceptor(InterceptorLevel.ADMIN)
135135
@RequestMapping(value = "/server/share", method = RequestMethod.POST)
136136
public String shareFile(String prefix, String files) {
137137
User user = (User) request.getSession().getAttribute(ValueConsts.USER_STRING);
138138
return ControllerUtils.getResponse(fileService.shareFiles(Checker.checkNull(prefix), files, user));
139139
}
140140

141+
@AuthInterceptor(InterceptorLevel.ADMIN)
142+
@RequestMapping(value = "/{id}/url", method = RequestMethod.PUT)
143+
public String uploadFileUrl(@PathVariable("id") int id, String oldLocalUrl, String localUrl, String visitUrl) {
144+
boolean[] b = fileService.updateUrl(id, oldLocalUrl, localUrl, visitUrl);
145+
String responseJson = "{status:{localUrl:" + b[0] + ",visitUrl:" + b[1] + "}}";
146+
return Formatter.formatJson(responseJson);
147+
}
148+
149+
@AuthInterceptor(InterceptorLevel.ADMIN)
150+
@RequestMapping(value = "/batch/{ids}", method = RequestMethod.DELETE)
151+
public String deleteFiles(@PathVariable("ids") String ids) {
152+
return ControllerUtils.getResponse(fileService.deleteFiles(ids));
153+
}
154+
155+
@AuthInterceptor(InterceptorLevel.ADMIN)
156+
@RequestMapping(value = "/{id}/auth", method = RequestMethod.GET)
157+
public String getAuth(@PathVariable("id") long id) {
158+
return BeanUtils.toPrettyJson(fileService.getAuth(id));
159+
}
160+
161+
@AuthInterceptor(InterceptorLevel.ADMIN)
162+
@RequestMapping(value = "/{id}/auth", method = RequestMethod.PUT)
163+
public String updateAuth(@PathVariable("id") long id, String auth) {
164+
return ControllerUtils.getResponse(fileService.updateAuth(id, auth));
165+
}
166+
141167
/**
142168
* 资源下载
143169
*

0 commit comments

Comments
 (0)