Skip to content

Commit a3bb652

Browse files
committed
FilesystemImagePersistenceStrategy: fix path to the file which prevents it to be saved to the disk.
Before MultipartFile.transferTo() tries to save the file relatively to directory from multipart-config/location (in web.xml). As result instead of put file to /data/uploads it tries to write it to /tmp/data/uploads and because intermediate directories doesn't exist it silently exits. See also my question at SO about spy objects: http://stackoverflow.com/questions/21562276/how-to-verify-argument-of-method-of-spying-object Addressed to #132
1 parent 42114fb commit a3bb652

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/main/java/ru/mystamps/web/service/FilesystemImagePersistenceStrategy.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
package ru.mystamps.web.service;
1919

2020
import java.io.File;
21+
import java.io.FileOutputStream;
2122
import java.io.IOException;
2223

2324
import javax.annotation.PostConstruct;
2425

2526
import org.apache.commons.io.FileUtils;
27+
import org.apache.commons.io.IOUtils;
2628

2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
@@ -65,15 +67,12 @@ public void init() {
6567
public void save(MultipartFile file, Image image) {
6668
try {
6769
File dest = createFile(image);
68-
file.transferTo(dest);
70+
writeToFile(file, dest);
6971

7072
LOG.debug("Image's data was written into file {}", dest);
7173

7274
} catch (IOException ex) {
7375
throw new ImagePersistenceException(ex);
74-
75-
} catch (IllegalStateException ex) {
76-
throw new ImagePersistenceException(ex);
7776
}
7877
}
7978

@@ -98,7 +97,15 @@ public ImageDto get(Image image) {
9897
protected File createFile(Image image) {
9998
return new File(storageDir, generateFileName(image));
10099
}
101-
100+
101+
// protected to allow spying
102+
protected void writeToFile(MultipartFile file, File dest) throws IOException {
103+
// we can't use file.transferTo(dest) there because it creates file
104+
// relatively to directory from multipart-config/location in web.xml
105+
// TODO(java7): use Files.copy() instead
106+
IOUtils.copy(file.getInputStream(), new FileOutputStream(dest));
107+
}
108+
102109
// protected to allow spying
103110
protected byte[] toByteArray(File dest) throws IOException {
104111
// TODO(guava): use Files.toByteArray()

src/test/groovy/ru/mystamps/web/service/FilesystemImagePersistenceStrategyTest.groovy

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
4242
when:
4343
strategy.save(multipartFile, image)
4444
then:
45-
1 * multipartFile.transferTo(_ as File)
45+
1 * strategy.writeToFile(_ as MultipartFile, _ as File) >> {}
4646
}
4747

4848
def "save() should saves file onto the configured directory"() {
@@ -51,10 +51,10 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
5151
when:
5252
strategy.save(multipartFile, image)
5353
then:
54-
1 * multipartFile.transferTo({File file ->
54+
1 * strategy.writeToFile(_ as MultipartFile, { File file ->
5555
assert file.parent == expectedDirectoryName
5656
return true
57-
})
57+
}) >> {}
5858
}
5959

6060
def "save() should gives proper name to the file"() {
@@ -65,15 +65,15 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
6565
when:
6666
strategy.save(multipartFile, image)
6767
then:
68-
1 * multipartFile.transferTo({File file ->
68+
1 * strategy.writeToFile(_ as MultipartFile, { File file ->
6969
assert file.name == expectedFileName
7070
return true
71-
})
71+
}) >> {}
7272
}
7373

7474
def "save() should converts IOException to ImagePersistenceException"() {
7575
given:
76-
multipartFile.transferTo(_ as File) >> { throw new IOException() }
76+
strategy.writeToFile(_ as MultipartFile, _ as File) >> { throw new IOException() }
7777
when:
7878
strategy.save(multipartFile, image)
7979
then:
@@ -82,17 +82,6 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
8282
ex.cause instanceof IOException
8383
}
8484

85-
def "save() should converts IllegalStateException to ImagePersistenceException"() {
86-
given:
87-
multipartFile.transferTo(_ as File) >> { throw new IllegalStateException() }
88-
when:
89-
strategy.save(multipartFile, image)
90-
then:
91-
ImagePersistenceException ex = thrown()
92-
and:
93-
ex.cause instanceof IllegalStateException
94-
}
95-
9685
//
9786
// Tests for get()
9887
//

0 commit comments

Comments
 (0)