/
DefaultPictureAdapter.java
197 lines (170 loc) · 7.2 KB
/
DefaultPictureAdapter.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Contributors:
* Nuxeo - initial API and implementation
*
* $Id$
*/
package org.nuxeo.ecm.platform.picture.api.adapters;
import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_INFO_PROPERTY;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.Blobs;
import org.nuxeo.ecm.core.api.PropertyException;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.platform.picture.api.ImageInfo;
import org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants;
import org.nuxeo.ecm.platform.picture.api.ImagingService;
import org.nuxeo.ecm.platform.picture.api.PictureConversion;
import org.nuxeo.ecm.platform.picture.api.PictureView;
import org.nuxeo.runtime.api.Framework;
public class DefaultPictureAdapter extends AbstractPictureAdapter {
private static final Log log = LogFactory.getLog(DefaultPictureAdapter.class);
// needs trailing slash
private static final String VIEW_XPATH = "picture:views/item[%d]/";
private static final String TITLE_PROPERTY = "title";
private static final String FILENAME_PROPERTY = "filename";
@Override
public boolean createPicture(Blob blob, String filename, String title,
ArrayList<Map<String, Object>> pictureConversions) throws IOException {
return fillPictureViews(blob, filename, title, pictureConversions);
}
@Override
public boolean fillPictureViews(Blob blob, String filename, String title,
ArrayList<Map<String, Object>> pictureConversions) throws IOException {
if (blob == null) {
clearViews();
return true;
}
File file = blob.getFile();
if (file == null) {
file = File.createTempFile("nuxeoImage", ".jpg");
Framework.trackFile(file, this);
blob.transferTo(file);
// use a persistent blob with our file
String digest = blob.getDigest();
blob = Blobs.createBlob(file, blob.getMimeType(), blob.getEncoding(), blob.getFilename());
blob.setDigest(digest);
}
fileContent = blob;
type = blob.getMimeType();
if (type == null || type.equals("application/octet-stream")) {
// TODO : use MimetypeRegistry instead
type = getImagingService().getImageMimeType(file);
blob.setMimeType(type);
}
if (type == null || type.equals("application/octet-stream")) {
return false;
}
ImageInfo imageInfo = getImageInfo();
if (imageInfo != null) {
doc.setPropertyValue(PICTURE_INFO_PROPERTY, (Serializable) imageInfo.toMap());
}
if (imageInfo != null) {
width = imageInfo.getWidth();
height = imageInfo.getHeight();
depth = imageInfo.getDepth();
}
if (width != null && height != null) {
clearViews();
addViews(pictureConversions, filename, title);
}
return true;
}
@Override
public void preFillPictureViews(Blob blob, List<Map<String, Object>> pictureConversions, ImageInfo imageInfo)
throws IOException {
ImagingService imagingService = getImagingService();
List<PictureView> pictureViews;
if (pictureConversions != null) {
List<PictureConversion> conversions = new ArrayList<PictureConversion>(pictureConversions.size());
for (Map<String, Object> template : pictureConversions) {
conversions.add(new PictureConversion((String) template.get("title"),
(String) template.get("description"), (String) template.get("tag"), 0));
}
pictureViews = imagingService.computeViewsFor(blob, conversions, imageInfo, false);
} else {
pictureViews = imagingService.computeViewsFor(doc, blob, imageInfo, false);
}
addPictureViews(pictureViews, true);
}
@Override
public void doRotate(int angle) {
int size = doc.getProperty(VIEWS_PROPERTY).size();
for (int i = 0; i < size; i++) {
String xpath = "picture:views/view[" + i + "]/";
BlobHolder blob = new SimpleBlobHolder(doc.getProperty(xpath + "content").getValue(Blob.class));
String type = blob.getBlob().getMimeType();
if (type != "image/png") {
Map<String, Serializable> options = new HashMap<String, Serializable>();
options.put(ImagingConvertConstants.OPTION_ROTATE_ANGLE, angle);
blob = getConversionService().convert(ImagingConvertConstants.OPERATION_ROTATE, blob, options);
doc.getProperty(xpath + "content").setValue(blob.getBlob());
Long height = (Long) doc.getProperty(xpath + "height").getValue();
Long width = (Long) doc.getProperty(xpath + "width").getValue();
doc.getProperty(xpath + "height").setValue(width);
doc.getProperty(xpath + "width").setValue(height);
}
}
}
@Override
public void doCrop(String coords) {
doc.setPropertyValue("picture:cropCoords", coords);
}
@Override
public Blob getPictureFromTitle(String title) throws PropertyException {
if (title == null) {
return null;
}
Collection<Property> views = doc.getProperty(VIEWS_PROPERTY).getChildren();
for (Property property : views) {
if (title.equals(property.getValue(TITLE_PROPERTY))) {
Blob blob = (Blob) property.getValue("content");
if (blob != null) {
blob.setFilename((String) property.getValue(FILENAME_PROPERTY));
}
return blob;
}
}
return null;
}
@Override
public String getFirstViewXPath() {
return getViewXPathFor(0);
}
@Override
public String getViewXPath(String viewName) {
Property views = doc.getProperty(VIEWS_PROPERTY);
for (int i = 0; i < views.size(); i++) {
if (views.get(i).getValue(TITLE_PROPERTY).equals(viewName)) {
return getViewXPathFor(i);
}
}
return null;
}
protected String getViewXPathFor(int index) {
return String.format(VIEW_XPATH, index);
}
}