Using java.sql.Blob and Hibernate specific classes for handling binary data.
parent
65425ee88f
commit
d0bad06c21
|
|
@ -87,7 +87,7 @@ public abstract class AbstractBinaryAssetFormController<T extends BinaryAsset>
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.containsKey(DATA)) {
|
if (data.containsKey(DATA)) {
|
||||||
asset.setData((byte[]) data.get(DATA));
|
//asset.setData((byte[]) data.get(DATA));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.containsKey(SIZE)) {
|
if (data.containsKey(SIZE)) {
|
||||||
|
|
|
||||||
|
|
@ -35,9 +35,16 @@ import javax.persistence.Lob;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
|
import org.hibernate.envers.NotAudited;
|
||||||
|
import org.libreccm.core.UnexpectedErrorException;
|
||||||
import org.libreccm.jpa.utils.MimeTypeConverter;
|
import org.libreccm.jpa.utils.MimeTypeConverter;
|
||||||
import org.libreccm.l10n.LocalizedString;
|
import org.libreccm.l10n.LocalizedString;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.sql.Blob;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import javax.persistence.Basic;
|
import javax.persistence.Basic;
|
||||||
import javax.persistence.Convert;
|
import javax.persistence.Convert;
|
||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
|
|
@ -79,7 +86,9 @@ public class BinaryAsset extends Asset implements Serializable {
|
||||||
@Column(name = "ASSET_DATA")
|
@Column(name = "ASSET_DATA")
|
||||||
@Lob
|
@Lob
|
||||||
@Basic(fetch = FetchType.LAZY)
|
@Basic(fetch = FetchType.LAZY)
|
||||||
private byte[] data;
|
@NotAudited
|
||||||
|
// private byte[] data;
|
||||||
|
private Blob data;
|
||||||
|
|
||||||
@Column(name = "DATA_SIZE")
|
@Column(name = "DATA_SIZE")
|
||||||
private long size;
|
private long size;
|
||||||
|
|
@ -87,7 +96,7 @@ public class BinaryAsset extends Asset implements Serializable {
|
||||||
public BinaryAsset() {
|
public BinaryAsset() {
|
||||||
super();
|
super();
|
||||||
description = new LocalizedString();
|
description = new LocalizedString();
|
||||||
data = new byte[]{};
|
//data = new byte[]{};
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalizedString getDescription() {
|
public LocalizedString getDescription() {
|
||||||
|
|
@ -115,21 +124,52 @@ public class BinaryAsset extends Asset implements Serializable {
|
||||||
this.mimeType = mimeType;
|
this.mimeType = mimeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getData() {
|
// public byte[] getData() {
|
||||||
if (data == null) {
|
// if (data == null) {
|
||||||
return new byte[]{};
|
// return new byte[]{};
|
||||||
} else {
|
// } else {
|
||||||
return Arrays.copyOf(data, data.length);
|
// return Arrays.copyOf(data, data.length);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setData(final byte[] data) {
|
||||||
|
// if (data == null) {
|
||||||
|
// this.data = new byte[]{};
|
||||||
|
// size = this.data.length;
|
||||||
|
// } else {
|
||||||
|
// this.data = Arrays.copyOf(data, data.length);
|
||||||
|
// size = data.length;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
public Blob getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDataSize() {
|
||||||
|
try {
|
||||||
|
return data.length();
|
||||||
|
} catch(SQLException ex) {
|
||||||
|
throw new UnexpectedErrorException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setData(final byte[] data) {
|
public InputStream getDataAsInputStream() {
|
||||||
if (data == null) {
|
try {
|
||||||
this.data = new byte[]{};
|
return data.getBinaryStream();
|
||||||
size = this.data.length;
|
} catch (SQLException ex) {
|
||||||
} else {
|
throw new UnexpectedErrorException(ex);
|
||||||
this.data = Arrays.copyOf(data, data.length);
|
}
|
||||||
size = data.length;
|
}
|
||||||
|
|
||||||
|
public void setData(final Blob data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutputStream getDataOutputStream() {
|
||||||
|
try {
|
||||||
|
return data.setBinaryStream(0);
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
throw new UnexpectedErrorException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,7 +187,7 @@ public class BinaryAsset extends Asset implements Serializable {
|
||||||
hash = 59 * hash + Objects.hashCode(description);
|
hash = 59 * hash + Objects.hashCode(description);
|
||||||
hash = 59 * hash + Objects.hashCode(fileName);
|
hash = 59 * hash + Objects.hashCode(fileName);
|
||||||
hash = 59 * hash + Objects.hashCode(mimeType);
|
hash = 59 * hash + Objects.hashCode(mimeType);
|
||||||
hash = 59 * hash + Arrays.hashCode(data);
|
hash = 59 * hash + Objects.hashCode(data);
|
||||||
hash = 59 * hash + (int) (size ^ (size >>> 32));
|
hash = 59 * hash + (int) (size ^ (size >>> 32));
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
@ -183,7 +223,7 @@ public class BinaryAsset extends Asset implements Serializable {
|
||||||
if (!Objects.equals(mimeType, other.getMimeType())) {
|
if (!Objects.equals(mimeType, other.getMimeType())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Arrays.equals(data, other.getData());
|
return Objects.equals(data, other.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -257,10 +257,11 @@ public class Images {
|
||||||
final String widthParam,
|
final String widthParam,
|
||||||
final String heightParam) {
|
final String heightParam) {
|
||||||
|
|
||||||
final byte[] data = image.getData();
|
//final byte[] data = image.getData();
|
||||||
final String mimeType = image.getMimeType().toString();
|
final String mimeType = image.getMimeType().toString();
|
||||||
|
|
||||||
final InputStream inputStream = new ByteArrayInputStream(data);
|
//final InputStream inputStream = new ByteArrayInputStream(data);
|
||||||
|
final InputStream inputStream = image.getDataAsInputStream();
|
||||||
final BufferedImage bufferedImage;
|
final BufferedImage bufferedImage;
|
||||||
final String imageFormat;
|
final String imageFormat;
|
||||||
try {
|
try {
|
||||||
|
|
@ -334,10 +335,11 @@ public class Images {
|
||||||
*/
|
*/
|
||||||
private Response readImageProperties(final Image image) {
|
private Response readImageProperties(final Image image) {
|
||||||
|
|
||||||
final byte[] data = image.getData();
|
//final byte[] data = image.getData();
|
||||||
final String mimeType = image.getMimeType().toString();
|
final String mimeType = image.getMimeType().toString();
|
||||||
|
|
||||||
final InputStream inputStream = new ByteArrayInputStream(data);
|
//final InputStream inputStream = new ByteArrayInputStream(data);
|
||||||
|
final InputStream inputStream = image.getDataAsInputStream();
|
||||||
final BufferedImage bufferedImage;
|
final BufferedImage bufferedImage;
|
||||||
try {
|
try {
|
||||||
bufferedImage = ImageIO.read(inputStream);
|
bufferedImage = ImageIO.read(inputStream);
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,9 @@ public class ImagesPropertiesProvider implements AssetPropertiesProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
final Image image = (Image) asset;
|
final Image image = (Image) asset;
|
||||||
final byte[] data = image.getData();
|
// final byte[] data = image.getData();
|
||||||
final InputStream inputStream = new ByteArrayInputStream(data);
|
// final InputStream inputStream = new ByteArrayInputStream(data);
|
||||||
|
final InputStream inputStream = image.getDataAsInputStream();
|
||||||
final BufferedImage bufferedImage;
|
final BufferedImage bufferedImage;
|
||||||
try {
|
try {
|
||||||
bufferedImage = ImageIO.read(inputStream);
|
bufferedImage = ImageIO.read(inputStream);
|
||||||
|
|
@ -70,7 +71,8 @@ public class ImagesPropertiesProvider implements AssetPropertiesProvider {
|
||||||
.add("mimetype", image.getMimeType().toString())
|
.add("mimetype", image.getMimeType().toString())
|
||||||
.add("width", bufferedImage.getWidth())
|
.add("width", bufferedImage.getWidth())
|
||||||
.add("height", bufferedImage.getHeight())
|
.add("height", bufferedImage.getHeight())
|
||||||
.add("size", data.length);
|
.add("size", image.getDataSize());
|
||||||
|
// .add("size", data.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ package org.librecms.ui.contentsections.assets;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.hibernate.engine.jdbc.BlobProxy;
|
||||||
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
|
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
|
||||||
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
|
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
|
||||||
import org.libreccm.core.UnexpectedErrorException;
|
import org.libreccm.core.UnexpectedErrorException;
|
||||||
|
|
@ -31,8 +32,12 @@ import org.librecms.contentsection.AssetRepository;
|
||||||
import org.librecms.ui.contentsections.AssetPermissionsChecker;
|
import org.librecms.ui.contentsections.AssetPermissionsChecker;
|
||||||
import org.librecms.ui.contentsections.ContentSectionNotFoundException;
|
import org.librecms.ui.contentsections.ContentSectionNotFoundException;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.sql.Blob;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -46,7 +51,6 @@ import javax.enterprise.context.RequestScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.mvc.Controller;
|
import javax.mvc.Controller;
|
||||||
import javax.mvc.Models;
|
import javax.mvc.Models;
|
||||||
import javax.servlet.http.Part;
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.FormParam;
|
import javax.ws.rs.FormParam;
|
||||||
|
|
@ -56,7 +60,6 @@ import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -353,33 +356,65 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
||||||
.getHeaders();
|
.getHeaders();
|
||||||
fileName = getFileName(headers);
|
fileName = getFileName(headers);
|
||||||
contentType = getContentType(headers);
|
contentType = getContentType(headers);
|
||||||
fileSize = getFileSize(headers);
|
// fileSize = getFileSize(headers);
|
||||||
|
|
||||||
// fileAsset.setFileName(fileName);
|
final java.nio.file.Path tmpFilePath = Files
|
||||||
// //fileAsset.setSize(fileAsset.getData().length);
|
.createTempFile(
|
||||||
// try {
|
"upload", fileName
|
||||||
// fileAsset.setMimeType(new MimeType(contentType));
|
|
||||||
// } catch (MimeTypeParseException ex) {
|
|
||||||
// LOGGER.error(
|
|
||||||
// "Failed to upload file for FileAsset {}:", assetPath
|
|
||||||
// );
|
|
||||||
// LOGGER.error(ex);
|
|
||||||
//
|
|
||||||
// models.put("uploadFailed", true);
|
|
||||||
// return buildRedirectPathForStep();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// assetRepo.save(fileAsset);
|
|
||||||
try ( InputStream inputStream = inputPart.getBody(
|
|
||||||
InputStream.class, null)) {
|
|
||||||
dataService.saveData(
|
|
||||||
fileAsset,
|
|
||||||
inputStream,
|
|
||||||
fileName,
|
|
||||||
contentType,
|
|
||||||
fileSize
|
|
||||||
);
|
);
|
||||||
|
try ( InputStream inputStream = inputPart.getBody(
|
||||||
|
InputStream.class, null
|
||||||
|
)) {
|
||||||
|
try ( OutputStream outputStream = Files.newOutputStream(
|
||||||
|
tmpFilePath
|
||||||
|
)) {
|
||||||
|
int length;
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
while ((length = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer);
|
||||||
|
fileSize += length;
|
||||||
}
|
}
|
||||||
|
outputStream.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// try ( InputStream fileInputStream = Files.newInputStream(
|
||||||
|
// tmpFilePath
|
||||||
|
// )) {
|
||||||
|
|
||||||
|
final Blob data = BlobProxy.generateProxy(
|
||||||
|
Files.newInputStream(tmpFilePath), -1
|
||||||
|
);
|
||||||
|
fileAsset.setFileName(fileName);
|
||||||
|
fileAsset.setData(data);
|
||||||
|
|
||||||
|
fileAsset.setSize(fileSize);
|
||||||
|
// fileAsset.setSize(fileAsset.getData().length);
|
||||||
|
try {
|
||||||
|
fileAsset.setMimeType(new MimeType(contentType));
|
||||||
|
} catch (MimeTypeParseException ex) {
|
||||||
|
LOGGER.error(
|
||||||
|
"Failed to upload file for FileAsset {}:",
|
||||||
|
assetPath
|
||||||
|
);
|
||||||
|
LOGGER.error(ex);
|
||||||
|
|
||||||
|
models.put("uploadFailed", true);
|
||||||
|
return buildRedirectPathForStep();
|
||||||
|
}
|
||||||
|
|
||||||
|
assetRepo.save(fileAsset);
|
||||||
|
// }
|
||||||
|
// try ( InputStream inputStream = inputPart.getBody(
|
||||||
|
// InputStream.class, null)) {
|
||||||
|
// dataService.saveData(
|
||||||
|
// fileAsset,
|
||||||
|
// inputStream,
|
||||||
|
// fileName,
|
||||||
|
// contentType,
|
||||||
|
// fileSize
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LOGGER.error(
|
LOGGER.error(
|
||||||
|
|
@ -399,6 +434,7 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
||||||
getAsset(),
|
getAsset(),
|
||||||
messageBundle.get("asset.edit.denied"));
|
messageBundle.get("asset.edit.denied"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFileName(final MultivaluedMap<String, String> headers) {
|
private String getFileName(final MultivaluedMap<String, String> headers) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue