@@ -39,9 +39,11 @@ public class FilesystemImagePersistenceStrategy implements ImagePersistenceStrat
39
39
LoggerFactory .getLogger (FilesystemImagePersistenceStrategy .class );
40
40
41
41
private final File storageDir ;
42
+ private final File previewDir ;
42
43
43
- public FilesystemImagePersistenceStrategy (String storageDir ) {
44
+ public FilesystemImagePersistenceStrategy (String storageDir , String previewDir ) {
44
45
this .storageDir = new File (storageDir );
46
+ this .previewDir = new File (previewDir );
45
47
}
46
48
47
49
@ PostConstruct
@@ -53,11 +55,29 @@ public void init() {
53
55
54
56
} else if (!storageDir .canWrite ()) {
55
57
LOG .warn (
58
+ // TODO(java9): log also user: ProcessHandle.current().info().user()
56
59
"Directory '{}' exists but isn't writable for the current user! "
57
60
+ "Image uploading won't work." ,
58
61
storageDir
59
62
);
60
63
}
64
+
65
+ LOG .info ("Image previews will be saved into {} directory" , previewDir );
66
+
67
+ if (!previewDir .exists ()) { // NOPMD: ConfusingTernary (it's ok for me)
68
+ LOG .warn (
69
+ "Directory '{}' doesn't exist! Image preview generation won't work" ,
70
+ previewDir
71
+ );
72
+
73
+ } else if (!previewDir .canWrite ()) {
74
+ // TODO(java9): log also user: ProcessHandle.current().info().user()
75
+ LOG .warn (
76
+ "Directory '{}' exists but isn't writable for the current user! "
77
+ + "Image preview generation won't work" ,
78
+ previewDir
79
+ );
80
+ }
61
81
}
62
82
63
83
@ Override
@@ -73,9 +93,27 @@ public void save(MultipartFile file, ImageInfoDto image) {
73
93
}
74
94
}
75
95
96
+ @ Override
97
+ public void savePreview (byte [] data , ImageInfoDto image ) {
98
+ try {
99
+ Path dest = generateFilePath (previewDir , image );
100
+ writeToFile (data , dest );
101
+
102
+ LOG .info ("Image preview data has been written into file {}" , dest );
103
+
104
+ } catch (IOException ex ) {
105
+ throw new ImagePersistenceException (ex );
106
+ }
107
+ }
108
+
76
109
@ Override
77
110
public ImageDto get (ImageInfoDto image ) {
78
- return get (storageDir , image );
111
+ return get (storageDir , image , true );
112
+ }
113
+
114
+ @ Override
115
+ public ImageDto getPreview (ImageInfoDto image ) {
116
+ return get (previewDir , image , false );
79
117
}
80
118
81
119
// protected to allow spying
@@ -92,6 +130,11 @@ protected void writeToFile(MultipartFile file, Path dest) throws IOException {
92
130
Files .copy (file .getInputStream (), dest );
93
131
}
94
132
133
+ // protected to allow spying
134
+ protected void writeToFile (byte [] data , Path dest ) throws IOException {
135
+ Files .write (dest , data );
136
+ }
137
+
95
138
// protected to allow spying
96
139
protected boolean exists (Path path ) {
97
140
return Files .exists (path );
@@ -102,10 +145,16 @@ protected byte[] toByteArray(Path dest) throws IOException {
102
145
return Files .readAllBytes (dest );
103
146
}
104
147
105
- private ImageDto get (File dir , ImageInfoDto image ) {
148
+ private ImageDto get (File dir , ImageInfoDto image , boolean logWarning ) {
106
149
Path dest = generateFilePath (dir , image );
107
150
if (!exists (dest )) {
108
- LOG .warn ("Found image without content: #{} ({} doesn't exist)" , image .getId (), dest );
151
+ if (logWarning ) {
152
+ LOG .warn (
153
+ "Image #{}: content not found ({} doesn't exist)" ,
154
+ image .getId (),
155
+ dest
156
+ );
157
+ }
109
158
return null ;
110
159
}
111
160
0 commit comments