import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
import org.argeo.cms.ui.CmsImageManager;
import org.argeo.jcr.JcrUtils;
import org.eclipse.rap.rwt.RWT;
} else if (constraints.x == 0) {// force height
return new Point(scale(orig.x, orig.y, constraints.y), constraints.y);
}
- throw new CmsException("Cannot resize " + orig + " to " + constraints);
+ throw new IllegalArgumentException("Cannot resize " + orig + " to " + constraints);
}
private int scale(int origDimension, int otherDimension, int otherConstraint) {
}
public Point getImageSize(Node node) throws RepositoryException {
- // TODO load the SWT image ?
- return new Point(0, 0);
+ // TODO optimise
+ Image image = getSwtImage(node);
+ return new Point(image.getBounds().width, image.getBounds().height);
}
/** @return null if not available */
@Override
public String getImageUrl(Node node) throws RepositoryException {
return CmsUiUtils.getDataPath(node);
- // String name = getResourceName(node);
- // ResourceManager resourceManager = RWT.getResourceManager();
- // if (!resourceManager.isRegistered(name)) {
- // InputStream inputStream = null;
- // Binary binary = getImageBinary(node);
- // if (binary == null)
- // return null;
- // try {
- // inputStream = binary.getStream();
- // resourceManager.register(name, inputStream);
- // } finally {
- // IOUtils.closeQuietly(inputStream);
- // JcrUtils.closeQuietly(binary);
- // }
- // if (log.isTraceEnabled())
- // log.trace("Registered image " + name);
- // }
- // return resourceManager.getLocation(name);
}
protected String getResourceName(Node node) throws RepositoryException {
}
@Override
- public String uploadImage(Node parentNode, String fileName, InputStream in) throws RepositoryException {
+ public String uploadImage(Node context, Node parentNode, String fileName, InputStream in, String contentType)
+ throws RepositoryException {
InputStream inputStream = null;
try {
String previousResourceName = null;
Node fileNode = JcrUtils.copyBytesAsFile(parentNode, fileName, arr);
inputStream = new ByteArrayInputStream(arr);
ImageData id = new ImageData(inputStream);
- processNewImageFile(fileNode, id);
+ processNewImageFile(context, fileNode, id);
- String mime = Files.probeContentType(Paths.get(fileName));
- fileNode.setProperty(Property.JCR_MIMETYPE, mime);
+ String mime = contentType != null ? contentType : Files.probeContentType(Paths.get(fileName));
+ if (mime != null) {
+ fileNode.getNode(JCR_CONTENT).setProperty(Property.JCR_MIMETYPE, mime);
+ }
fileNode.getSession().save();
// reset resource manager
if (log.isDebugEnabled())
log.debug("Unregistered image " + previousResourceName);
}
- return getImageUrl(fileNode);
+ return CmsUiUtils.getDataPath(fileNode);
} catch (IOException e) {
- throw new CmsException("Cannot upload image " + fileName + " in " + parentNode, e);
+ throw new RuntimeException("Cannot upload image " + fileName + " in " + parentNode, e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}
- /** Does nothign by default. */
- protected void processNewImageFile(Node fileNode, ImageData id) throws RepositoryException, IOException {
+ /** Does nothing by default. */
+ protected void processNewImageFile(Node context, Node fileNode, ImageData id)
+ throws RepositoryException, IOException {
}
}