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