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.dam.app.ui.field.upload;
35
36 import info.magnolia.cms.core.Path;
37 import info.magnolia.dam.app.DamAppConfiguration;
38 import info.magnolia.dam.app.ui.field.configuration.EditAssetAppConfiguration;
39 import info.magnolia.i18nsystem.SimpleTranslator;
40 import info.magnolia.ui.form.field.upload.UploadReceiver;
41
42 import java.io.InputStream;
43 import java.io.OutputStream;
44
45 import javax.inject.Inject;
46
47 import org.apache.commons.io.IOUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.google.common.net.MediaType;
52
53
54
55
56 public class AssetUploadReceiver extends UploadReceiver {
57 private static final Logger log = LoggerFactory.getLogger(AssetUploadReceiver.class);
58
59 private final DamAppConfiguration appConfig;
60 private ImageSize imageSize;
61 private long duration;
62 private EditAssetAppConfiguration editConfig;
63
64 @Inject
65 public AssetUploadReceiver(SimpleTranslator i18n, DamAppConfiguration damAppConfig) {
66 super(Path.getTempDirectory(), i18n);
67 this.appConfig = damAppConfig;
68 }
69
70
71
72
73 @Override
74 public void setValue(Object newValue) {
75 super.setValue(newValue);
76
77 updateRelatedProperties();
78 }
79
80
81
82
83 public boolean isImage() {
84 return getMimeType() != null && MediaType.parse(getMimeType()).is(MediaType.ANY_IMAGE_TYPE);
85 }
86
87
88
89
90 public long getHeight() {
91 return (imageSize != null) ? imageSize.getHeight() : 0;
92 }
93
94
95
96
97 public long getWidth() {
98 return (imageSize != null) ? imageSize.getWidth() : 0;
99 }
100
101
102
103
104 public long getDuration() {
105 return duration;
106 }
107
108
109
110
111 public void updateContent(InputStream in) {
112 OutputStream out = receiveUpload(getFileName(), getMimeType());
113 try {
114 IOUtils.copy(in, out);
115 } catch (Exception e) {
116 log.warn("Not able to update the file content", e);
117 } finally {
118 IOUtils.closeQuietly(in);
119 IOUtils.closeQuietly(out);
120 }
121 updateRelatedProperties();
122 }
123
124 public EditAssetAppConfiguration getEditAssetAppConfiguration() {
125 return this.editConfig;
126 }
127
128
129
130
131 public void updateRelatedProperties() {
132 reloadEditAssetAppConfiguration();
133 if (!isEmpty() && isImage()) {
134 imageSize = ImageSize.valueOf(getContentAsStream());
135 duration = 0;
136 } else {
137
138 duration = 0;
139 imageSize = null;
140 }
141 }
142
143
144
145
146 private void reloadEditAssetAppConfiguration() {
147 try {
148 if (this.getMimeType() != null) {
149 editConfig = appConfig.getEditAssetAppConfigurationForMediaType(MediaType.parse(this.getMimeType()));
150 return;
151 }
152 log.warn("Could not set EditAssetAppConfiguration based on the MimeType '{}'. Upload Field may not work property", this.getMimeType());
153
154 } catch (IllegalArgumentException e) {
155 log.warn("Could not get a valid EditAssetAppConfiguration based on the MimeType '{}'. Upload Field may not work property", this.getMimeType());
156 }
157 editConfig = null;
158 }
159
160 }