1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.multipart;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.Serializable;
40
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.io.IOUtils;
43 import org.springframework.web.multipart.MultipartFile;
44
45 import info.magnolia.cms.beans.runtime.Document;
46
47
48
49
50
51
52
53
54 public class BlossomMultipartFile implements MultipartFile, Serializable {
55
56 private Document document;
57
58 public BlossomMultipartFile(Document document) {
59 this.document = document;
60 }
61
62 @Override
63 public byte[] getBytes() throws IOException {
64 return IOUtils.toByteArray(document.getStream());
65 }
66
67 @Override
68 public String getContentType() {
69 return document.getType();
70 }
71
72 @Override
73 public InputStream getInputStream() throws IOException {
74 return document.getStream();
75 }
76
77 @Override
78 public String getName() {
79 return document.getAtomName();
80 }
81
82 @Override
83 public String getOriginalFilename() {
84 return document.getFileNameWithExtension();
85 }
86
87 @Override
88 public long getSize() {
89 return document.getLength();
90 }
91
92 @Override
93 public boolean isEmpty() {
94 return document.getLength() <= 0;
95 }
96
97 @Override
98 public void transferTo(File destination) throws IOException, IllegalStateException {
99
100
101 if (destination.exists() && !destination.delete()) {
102 throw new IOException("Destination file [" + destination.getAbsolutePath() + "] already exists and could not be deleted");
103 }
104
105
106 FileUtils.copyFile(document.getFile(), destination);
107 }
108 }