27
27
import javax .annotation .PostConstruct ;
28
28
29
29
import org .slf4j .Logger ;
30
- import org .slf4j .LoggerFactory ;
31
30
32
31
import org .springframework .web .multipart .MultipartFile ;
33
32
37
36
import ru .mystamps .web .service .exception .ImagePersistenceException ;
38
37
39
38
public class FilesystemImagePersistenceStrategy implements ImagePersistenceStrategy {
40
- private static final Logger LOG =
41
- LoggerFactory .getLogger (FilesystemImagePersistenceStrategy .class );
42
39
40
+ private final Logger log ;
43
41
private final File storageDir ;
44
42
private final File previewDir ;
45
43
46
- public FilesystemImagePersistenceStrategy (String storageDir , String previewDir ) {
44
+ public FilesystemImagePersistenceStrategy (Logger logger , String storageDir , String previewDir ) {
45
+ this .log = logger ;
47
46
this .storageDir = new File (storageDir );
48
47
this .previewDir = new File (previewDir );
49
48
}
50
49
51
50
@ PostConstruct
52
51
public void init () {
53
- LOG .info ("Images will be saved into {} directory" , storageDir );
52
+ log .info ("Images will be saved into {} directory" , storageDir );
54
53
55
54
if (!storageDir .exists ()) { // NOPMD: ConfusingTernary (it's ok for me)
56
- LOG .warn ("Directory '{}' doesn't exist! Image uploading won't work." , storageDir );
55
+ log .warn ("Directory '{}' doesn't exist! Image uploading won't work." , storageDir );
57
56
58
57
} else if (!storageDir .canWrite ()) {
59
- LOG .warn (
58
+ log .warn (
60
59
// TODO(java9): log also user: ProcessHandle.current().info().user()
61
60
"Directory '{}' exists but isn't writable for the current user! "
62
61
+ "Image uploading won't work." ,
63
62
storageDir
64
63
);
65
64
}
66
65
67
- LOG .info ("Image previews will be saved into {} directory" , previewDir );
66
+ log .info ("Image previews will be saved into {} directory" , previewDir );
68
67
69
68
if (!previewDir .exists ()) { // NOPMD: ConfusingTernary (it's ok for me)
70
- LOG .warn (
69
+ log .warn (
71
70
"Directory '{}' doesn't exist! Image preview generation won't work" ,
72
71
previewDir
73
72
);
74
73
75
74
} else if (!previewDir .canWrite ()) {
76
75
// TODO(java9): log also user: ProcessHandle.current().info().user()
77
- LOG .warn (
76
+ log .warn (
78
77
"Directory '{}' exists but isn't writable for the current user! "
79
78
+ "Image preview generation won't work" ,
80
79
previewDir
@@ -88,7 +87,7 @@ public void save(MultipartFile file, ImageInfoDto image) {
88
87
Path dest = generateFilePath (storageDir , image );
89
88
writeToFile (file , dest );
90
89
91
- LOG .info ("Image data has been written into file {}" , dest );
90
+ log .info ("Image data has been written into file {}" , dest );
92
91
93
92
} catch (IOException ex ) {
94
93
throw new ImagePersistenceException (ex );
@@ -101,7 +100,7 @@ public void savePreview(byte[] data, ImageInfoDto image) {
101
100
Path dest = generateFilePath (previewDir , image );
102
101
writeToFile (data , dest );
103
102
104
- LOG .info ("Image preview data has been written into file {}" , dest );
103
+ log .info ("Image preview data has been written into file {}" , dest );
105
104
106
105
} catch (IOException ex ) {
107
106
throw new ImagePersistenceException (ex );
@@ -124,7 +123,7 @@ public void removeIfPossible(ImageInfoDto image) {
124
123
try {
125
124
Files .deleteIfExists (dest );
126
125
} catch (Exception ex ) { // NOPMD: AvoidCatchingGenericException
127
- LOG .warn ("Couldn't delete file {}: {}" , dest , ex .getMessage ());
126
+ log .warn ("Couldn't delete file {}: {}" , dest , ex .getMessage ());
128
127
}
129
128
}
130
129
@@ -164,7 +163,7 @@ private ImageDto get(File dir, ImageInfoDto image, boolean logWarning) {
164
163
Path dest = generateFilePath (dir , image );
165
164
if (!exists (dest )) {
166
165
if (logWarning ) {
167
- LOG .warn (
166
+ log .warn (
168
167
"Image #{}: content not found ({} doesn't exist)" ,
169
168
image .getId (),
170
169
dest
0 commit comments