5
5
import lombok .RequiredArgsConstructor ;
6
6
import lombok .SneakyThrows ;
7
7
import lombok .extern .slf4j .Slf4j ;
8
- import org .apache .commons .lang3 .RandomStringUtils ;
9
8
import org .springframework .http .HttpStatus ;
10
9
import org .springframework .http .MediaType ;
11
10
import org .springframework .util .StreamUtils ;
16
15
import xyz .erupt .annotation .sub_field .sub_edit .AttachmentType ;
17
16
import xyz .erupt .annotation .sub_field .sub_edit .HtmlEditorType ;
18
17
import xyz .erupt .core .annotation .EruptRouter ;
19
- import xyz .erupt .core .constant .EruptConst ;
20
18
import xyz .erupt .core .constant .EruptRestPath ;
21
19
import xyz .erupt .core .exception .EruptWebApiRuntimeException ;
22
20
import xyz .erupt .core .i18n .I18nTranslate ;
23
21
import xyz .erupt .core .prop .EruptProp ;
24
22
import xyz .erupt .core .service .EruptCoreService ;
25
- import xyz .erupt .core .util . DateUtil ;
23
+ import xyz .erupt .core .service . EruptFileService ;
26
24
import xyz .erupt .core .util .EruptUtil ;
27
25
import xyz .erupt .core .util .Erupts ;
28
26
import xyz .erupt .core .view .EruptApiModel ;
38
36
import java .io .InputStream ;
39
37
import java .nio .charset .Charset ;
40
38
import java .nio .charset .StandardCharsets ;
41
- import java .util .*;
39
+ import java .util .ArrayList ;
40
+ import java .util .HashMap ;
41
+ import java .util .List ;
42
+ import java .util .Map ;
42
43
import java .util .stream .Stream ;
43
44
44
45
/**
@@ -53,6 +54,8 @@ public class EruptFileController {
53
54
54
55
private final EruptProp eruptProp ;
55
56
57
+ private final EruptFileService eruptFileService ;
58
+
56
59
private static final String FS_SEP = "/" ;
57
60
58
61
@ SneakyThrows
@@ -63,15 +66,7 @@ public EruptApiModel upload(@PathVariable("erupt") String eruptName, @PathVariab
63
66
EruptModel eruptModel = EruptCoreService .getErupt (eruptName );
64
67
Erupts .powerLegal (eruptModel , powerObject -> powerObject .isEdit () || powerObject .isAdd ());
65
68
Edit edit = eruptModel .getEruptFieldMap ().get (fieldName ).getEruptField ().edit ();
66
- String path ;
67
- if (eruptProp .isKeepUploadFileName ()) {
68
- path = File .separator + DateUtil .getFormatDate (new Date (), DateUtil .DATE ) + File .separator + file .getOriginalFilename ()
69
- .replaceAll ("&|#|\\ ?|\\ s" , "" ).replace (edit .attachmentType ().fileSeparator (), "" );
70
- } else {
71
- String [] fileNameSplit = file .getOriginalFilename ().split ("\\ ." );
72
- path = File .separator + DateUtil .getFormatDate (new Date (), DateUtil .DATE )
73
- + File .separator + RandomStringUtils .randomAlphabetic (12 ) + EruptConst .DOT + fileNameSplit [fileNameSplit .length - 1 ];
74
- }
69
+ String path = eruptFileService .createPath (file ).replace (edit .attachmentType ().fileSeparator (), "" );
75
70
switch (edit .type ()) {
76
71
case ATTACHMENT :
77
72
AttachmentType attachmentType = edit .attachmentType ();
@@ -118,27 +113,8 @@ public EruptApiModel upload(@PathVariable("erupt") String eruptName, @PathVariab
118
113
}
119
114
break ;
120
115
}
121
- try {
122
- boolean localSave = true ;
123
- AttachmentProxy attachmentProxy = EruptUtil .findAttachmentProxy ();
124
- if (null != attachmentProxy ) {
125
- path = attachmentProxy .upLoad (file .getInputStream (), path .replace ("\\ " , "/" ));
126
- localSave = attachmentProxy .isLocalSave ();
127
- }
128
- if (localSave ) {
129
- File dest = new File (eruptProp .getUploadPath () + path );
130
- if (!dest .getParentFile ().exists ()) {
131
- if (!dest .getParentFile ().mkdirs ()) {
132
- return EruptApiModel .errorApi (I18nTranslate .$translate ("erupt.upload_error.cannot_created" ));
133
- }
134
- }
135
- file .transferTo (dest );
136
- }
137
- return EruptApiModel .successApi (path .replace ("\\ " , "/" ));
138
- } catch (Exception e ) {
139
- log .error ("erupt upload error" , e );
140
- return EruptApiModel .errorApi (I18nTranslate .$translate ("erupt.upload_error" ) + " " + e .getMessage ());
141
- }
116
+ return EruptApiModel .successApi (eruptFileService .upload (file , path ));
117
+
142
118
}
143
119
144
120
@@ -170,8 +146,10 @@ public Map<String, Object> uploadHtmlEditorImage(@PathVariable("erupt") String e
170
146
map .put ("url" , EruptRestPath .ERUPT_ATTACHMENT + eruptApiModel .getData ());
171
147
}
172
148
map .put ("uploaded" , true );
149
+ map .put ("state" , "SUCCESS" );
173
150
} else {
174
151
map .put ("uploaded" , false );
152
+ map .put ("state" , "ERROR" );
175
153
throw new EruptWebApiRuntimeException (eruptApiModel .getMessage ());
176
154
}
177
155
return map ;
@@ -194,9 +172,11 @@ public void uploadUEditorImage(@PathVariable("erupt") String eruptName,
194
172
response .getOutputStream ().write ((callback + "(" + json + ")" ).getBytes (StandardCharsets .UTF_8 ));
195
173
}
196
174
} else {
197
- Map <String , Object > map = uploadHtmlEditorImage (eruptName , fieldName , file );
198
- Boolean status = (Boolean ) map .get ("uploaded" );
199
- map .put ("state" , status ? "SUCCESS" : "ERROR" );
175
+ Map <String , Object > map = this .uploadHtmlEditorImage (eruptName , fieldName , file );
176
+ map .put ("filename" , file .getName ());
177
+ map .put ("original" , file .getOriginalFilename ());
178
+ map .put ("name" , file .getName ());
179
+ map .put ("size" , file .getSize ());
200
180
response .getOutputStream ().write (new Gson ().toJson (map ).getBytes (StandardCharsets .UTF_8 ));
201
181
}
202
182
}
0 commit comments