commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.file.modular.local; |
|
26 |
|
|
27 |
import cn.hutool.core.io.FileUtil; |
|
28 |
import cn.hutool.core.lang.Dict; |
|
29 |
import cn.hutool.core.util.StrUtil; |
|
30 |
import cn.hutool.system.SystemUtil; |
|
31 |
import vip.xiaonuo.core.file.FileOperator; |
|
32 |
import vip.xiaonuo.core.file.common.enums.BucketAuthEnum; |
|
33 |
import vip.xiaonuo.core.file.common.exp.FileServiceException; |
|
34 |
import vip.xiaonuo.core.file.modular.local.prop.LocalFileProperties; |
|
35 |
|
|
36 |
import java.io.File; |
|
37 |
import java.io.InputStream; |
|
38 |
|
|
39 |
/** |
|
40 |
* 阿里云文件操作 |
|
41 |
* |
|
42 |
* @author xuyuxiang |
|
43 |
* @date 2020/5/25 2:33 下午 |
|
44 |
*/ |
|
45 |
public class LocalFileOperator implements FileOperator { |
|
46 |
|
|
47 |
private final LocalFileProperties localFileProperties; |
|
48 |
|
|
49 |
private String currentSavePath = ""; |
|
50 |
|
|
51 |
private Dict localClient; |
|
52 |
|
|
53 |
public LocalFileOperator(LocalFileProperties localFileProperties) { |
|
54 |
this.localFileProperties = localFileProperties; |
|
55 |
initClient(); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public void initClient() { |
|
60 |
if (SystemUtil.getOsInfo().isWindows()) { |
|
61 |
String savePathWindows = localFileProperties.getLocalFileSavePathWin(); |
|
62 |
if (!FileUtil.exist(savePathWindows)) { |
|
63 |
FileUtil.mkdir(savePathWindows); |
|
64 |
} |
|
65 |
currentSavePath = savePathWindows; |
|
66 |
} else { |
|
67 |
String savePathLinux = localFileProperties.getLocalFileSavePathLinux(); |
|
68 |
if (!FileUtil.exist(savePathLinux)) { |
|
69 |
FileUtil.mkdir(savePathLinux); |
|
70 |
} |
|
71 |
currentSavePath = savePathLinux; |
|
72 |
} |
|
73 |
localClient = Dict.create(); |
|
74 |
localClient.put("currentSavePath", currentSavePath); |
|
75 |
localClient.put("localFileProperties", localFileProperties); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public void destroyClient() { |
|
80 |
// empty |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public Object getClient() { |
|
85 |
// empty |
|
86 |
return localClient; |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public boolean doesBucketExist(String bucketName) { |
|
91 |
String absolutePath = currentSavePath + File.separator + bucketName; |
|
92 |
return FileUtil.exist(absolutePath); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public void setBucketAcl(String bucketName, BucketAuthEnum bucketAuthEnum) { |
|
97 |
// empty |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public boolean isExistingFile(String bucketName, String key) { |
|
102 |
String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; |
|
103 |
return FileUtil.exist(absoluteFile); |
|
104 |
} |
|
105 |
|
|
106 |
@Override |
|
107 |
public void storageFile(String bucketName, String key, byte[] bytes) { |
|
108 |
|
|
109 |
// 判断bucket存在不存在 |
|
110 |
String bucketPath = currentSavePath + File.separator + bucketName; |
|
111 |
if (!FileUtil.exist(bucketPath)) { |
|
112 |
FileUtil.mkdir(bucketPath); |
|
113 |
} |
|
114 |
|
|
115 |
// 存储文件 |
|
116 |
String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; |
|
117 |
FileUtil.writeBytes(bytes, absoluteFile); |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public void storageFile(String bucketName, String key, InputStream inputStream) { |
|
122 |
|
|
123 |
// 判断bucket存在不存在 |
|
124 |
String bucketPath = currentSavePath + File.separator + bucketName; |
|
125 |
if (!FileUtil.exist(bucketPath)) { |
|
126 |
FileUtil.mkdir(bucketPath); |
|
127 |
} |
|
128 |
|
|
129 |
// 存储文件 |
|
130 |
String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; |
|
131 |
FileUtil.writeFromStream(inputStream, absoluteFile); |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public byte[] getFileBytes(String bucketName, String key) { |
|
136 |
|
|
137 |
// 判断文件存在不存在 |
|
138 |
String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; |
|
139 |
if (!FileUtil.exist(absoluteFile)) { |
|
140 |
String message = StrUtil.format("文件不存在,bucket={},key={}", bucketName, key); |
|
141 |
throw new FileServiceException(message); |
|
142 |
} else { |
|
143 |
return FileUtil.readBytes(absoluteFile); |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
@Override |
|
148 |
public void setFileAcl(String bucketName, String key, BucketAuthEnum bucketAuthEnum) { |
|
149 |
// empty |
|
150 |
} |
|
151 |
|
|
152 |
@Override |
|
153 |
public void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) { |
|
154 |
|
|
155 |
// 判断文件存在不存在 |
|
156 |
String originFile = currentSavePath + File.separator + originBucketName + File.separator + originFileKey; |
|
157 |
if (!FileUtil.exist(originFile)) { |
|
158 |
String message = StrUtil.format("源文件不存在,bucket={},key={}", originBucketName, originFileKey); |
|
159 |
throw new FileServiceException(message); |
|
160 |
} else { |
|
161 |
|
|
162 |
// 拷贝文件 |
|
163 |
String destFile = currentSavePath + File.separator + newBucketName + File.separator + newFileKey; |
|
164 |
FileUtil.copy(originFile, destFile, true); |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
@Override |
|
169 |
public String getFileAuthUrl(String bucketName, String key, Long timeoutMillis) { |
|
170 |
// empty |
|
171 |
return null; |
|
172 |
} |
|
173 |
|
|
174 |
@Override |
|
175 |
public void deleteFile(String bucketName, String key) { |
|
176 |
|
|
177 |
// 判断文件存在不存在 |
|
178 |
String file = currentSavePath + File.separator + bucketName + File.separator + key; |
|
179 |
if (!FileUtil.exist(file)) { |
|
180 |
return; |
|
181 |
} |
|
182 |
|
|
183 |
// 删除文件 |
|
184 |
FileUtil.del(file); |
|
185 |
|
|
186 |
} |
|
187 |
} |