Using java.sql.Blob and Hibernate specific classes for handling binary data.

pull/10/head
Jens Pelzetter 2021-06-26 19:12:10 +02:00
parent 65425ee88f
commit d0bad06c21
5 changed files with 130 additions and 50 deletions

View File

@ -87,7 +87,7 @@ public abstract class AbstractBinaryAssetFormController<T extends BinaryAsset>
}
if (data.containsKey(DATA)) {
asset.setData((byte[]) data.get(DATA));
//asset.setData((byte[]) data.get(DATA));
}
if (data.containsKey(SIZE)) {

View File

@ -35,9 +35,16 @@ import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.jpa.utils.MimeTypeConverter;
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.Convert;
import javax.persistence.FetchType;
@ -79,7 +86,9 @@ public class BinaryAsset extends Asset implements Serializable {
@Column(name = "ASSET_DATA")
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] data;
@NotAudited
// private byte[] data;
private Blob data;
@Column(name = "DATA_SIZE")
private long size;
@ -87,7 +96,7 @@ public class BinaryAsset extends Asset implements Serializable {
public BinaryAsset() {
super();
description = new LocalizedString();
data = new byte[]{};
//data = new byte[]{};
}
public LocalizedString getDescription() {
@ -115,21 +124,52 @@ public class BinaryAsset extends Asset implements Serializable {
this.mimeType = mimeType;
}
public byte[] getData() {
if (data == null) {
return new byte[]{};
} else {
return Arrays.copyOf(data, data.length);
// public byte[] getData() {
// if (data == null) {
// return new byte[]{};
// } else {
// 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) {
if (data == null) {
this.data = new byte[]{};
size = this.data.length;
} else {
this.data = Arrays.copyOf(data, data.length);
size = data.length;
public InputStream getDataAsInputStream() {
try {
return data.getBinaryStream();
} catch (SQLException ex) {
throw new UnexpectedErrorException(ex);
}
}
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(fileName);
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));
return hash;
}
@ -183,7 +223,7 @@ public class BinaryAsset extends Asset implements Serializable {
if (!Objects.equals(mimeType, other.getMimeType())) {
return false;
}
return Arrays.equals(data, other.getData());
return Objects.equals(data, other.getData());
}
@Override

View File

@ -257,10 +257,11 @@ public class Images {
final String widthParam,
final String heightParam) {
final byte[] data = image.getData();
//final byte[] data = image.getData();
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 String imageFormat;
try {
@ -334,10 +335,11 @@ public class Images {
*/
private Response readImageProperties(final Image image) {
final byte[] data = image.getData();
//final byte[] data = image.getData();
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;
try {
bufferedImage = ImageIO.read(inputStream);

View File

@ -55,8 +55,9 @@ public class ImagesPropertiesProvider implements AssetPropertiesProvider {
}
final Image image = (Image) asset;
final byte[] data = image.getData();
final InputStream inputStream = new ByteArrayInputStream(data);
// final byte[] data = image.getData();
// final InputStream inputStream = new ByteArrayInputStream(data);
final InputStream inputStream = image.getDataAsInputStream();
final BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(inputStream);
@ -70,7 +71,8 @@ public class ImagesPropertiesProvider implements AssetPropertiesProvider {
.add("mimetype", image.getMimeType().toString())
.add("width", bufferedImage.getWidth())
.add("height", bufferedImage.getHeight())
.add("size", data.length);
.add("size", image.getDataSize());
// .add("size", data.length);
}
}

View File

@ -20,6 +20,7 @@ package org.librecms.ui.contentsections.assets;
import org.apache.logging.log4j.LogManager;
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.MultipartFormDataInput;
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.ContentSectionNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.sql.Blob;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -46,7 +51,6 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.mvc.Controller;
import javax.mvc.Models;
import javax.servlet.http.Part;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
@ -56,7 +60,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
/**
*
@ -353,33 +356,65 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
.getHeaders();
fileName = getFileName(headers);
contentType = getContentType(headers);
fileSize = getFileSize(headers);
// fileSize = getFileSize(headers);
// fileAsset.setFileName(fileName);
// //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
final java.nio.file.Path tmpFilePath = Files
.createTempFile(
"upload", fileName
);
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) {
LOGGER.error(
@ -399,6 +434,7 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
getAsset(),
messageBundle.get("asset.edit.denied"));
}
}
private String getFileName(final MultivaluedMap<String, String> headers) {