Improvements for handling binary assets
parent
de65988459
commit
65425ee88f
|
|
@ -38,7 +38,9 @@ import org.hibernate.envers.Audited;
|
|||
import org.libreccm.jpa.utils.MimeTypeConverter;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Convert;
|
||||
import javax.persistence.FetchType;
|
||||
|
||||
import static org.librecms.CmsConstants.*;
|
||||
|
||||
|
|
@ -76,6 +78,7 @@ public class BinaryAsset extends Asset implements Serializable {
|
|||
|
||||
@Column(name = "ASSET_DATA")
|
||||
@Lob
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
private byte[] data;
|
||||
|
||||
@Column(name = "DATA_SIZE")
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import javax.annotation.Resource;
|
|||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Dependent;
|
||||
import javax.sql.DataSource;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -58,33 +59,45 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
|||
);
|
||||
stmt.setLong(1, asset.getObjectId());
|
||||
|
||||
try (ResultSet resultSet = stmt.executeQuery()) {
|
||||
try ( ResultSet resultSet = stmt.executeQuery()) {
|
||||
resultSet.next();
|
||||
final Blob blob = resultSet.getBlob("asset_data");
|
||||
try(InputStream inputStream = blob.getBinaryStream()) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int length;
|
||||
while((length = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, length);
|
||||
}
|
||||
}
|
||||
blob.getBinaryStream().transferTo(outputStream);
|
||||
// try ( InputStream inputStream = blob.getBinaryStream()) {
|
||||
// byte[] buffer = new byte[8192];
|
||||
// int length;
|
||||
// while ((length = inputStream.read(buffer)) != -1) {
|
||||
// outputStream.write(buffer, 0, length);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
} catch (SQLException |IOException ex) {
|
||||
} catch (SQLException | IOException ex) {
|
||||
throw new UnexpectedErrorException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveData(final BinaryAsset asset, final InputStream stream) {
|
||||
public void saveData(
|
||||
final BinaryAsset asset,
|
||||
final InputStream stream,
|
||||
final String fileName,
|
||||
final String mimeType,
|
||||
final long fileSize
|
||||
) {
|
||||
Objects.requireNonNull(asset, "Can't save data to null.");
|
||||
try ( Connection connection = dataSource.getConnection()) {
|
||||
final PreparedStatement stmt = connection
|
||||
.prepareStatement(
|
||||
"UPDATE ccm_cms.binary_assets SET asset_data = ? WHERE object_id = ?"
|
||||
"UPDATE ccm_cms.binary_assets SET asset_data = ?, filename = ?, mime_type = ?, data_size = ? WHERE object_id = ?"
|
||||
);
|
||||
stmt.setBlob(1, stream);
|
||||
stmt.setLong(2, asset.getObjectId());
|
||||
// connection.commit();
|
||||
stmt.setString(2, fileName);
|
||||
stmt.setString(3, mimeType);
|
||||
stmt.setLong(4, fileSize);
|
||||
stmt.setLong(5, asset.getObjectId());
|
||||
|
||||
stmt.execute();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new UnexpectedErrorException(ex);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,12 @@ public interface BinaryAssetDataProvider {
|
|||
BinaryAsset asset, OutputStream outputStream
|
||||
);
|
||||
|
||||
void saveData(BinaryAsset asset, InputStream stream);
|
||||
void saveData(
|
||||
BinaryAsset asset,
|
||||
InputStream stream,
|
||||
String fileName,
|
||||
String contentType,
|
||||
long fileSize
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,18 +45,25 @@ public class BinaryAssetDataService {
|
|||
private Instance<BinaryAssetDataProvider> dataProvider;
|
||||
|
||||
public void copyDataToOutputStream(
|
||||
final BinaryAsset asset, final OutputStream outputStream
|
||||
final BinaryAsset asset,
|
||||
final OutputStream outputStream
|
||||
) {
|
||||
Objects.requireNonNull(asset, "Can't retrieve data from null.");
|
||||
Objects.requireNonNull(outputStream, "Can't copy data to null.");
|
||||
getDataProvider().copyDataToOutputStream(asset, outputStream);
|
||||
}
|
||||
|
||||
public void saveData(final BinaryAsset asset, final InputStream stream) {
|
||||
public void saveData(
|
||||
final BinaryAsset asset,
|
||||
final InputStream stream,
|
||||
final String fileName,
|
||||
final String mimeType,
|
||||
final long fileSize
|
||||
) {
|
||||
Objects.requireNonNull(asset, "Can't save data to null.");
|
||||
|
||||
final BinaryAssetDataProvider dataProvider = getDataProvider();
|
||||
dataProvider.saveData(asset, stream);
|
||||
dataProvider.saveData(asset, stream, fileName, mimeType, fileSize);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
|
||||
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
|
||||
import org.libreccm.core.UnexpectedErrorException;
|
||||
import org.libreccm.l10n.GlobalizationHelper;
|
||||
import org.libreccm.security.AuthorizationRequired;
|
||||
import org.librecms.assets.BinaryAssetDataService;
|
||||
|
|
@ -345,33 +346,41 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
|
||||
String fileName = "";
|
||||
String contentType = "";
|
||||
long fileSize = 0;
|
||||
for (final InputPart inputPart : inputParts) {
|
||||
try {
|
||||
final MultivaluedMap<String, String> headers = inputPart
|
||||
.getHeaders();
|
||||
fileName = getFileName(headers);
|
||||
contentType = getContentType(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);
|
||||
|
||||
// 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);
|
||||
dataService.saveData(
|
||||
fileAsset,
|
||||
inputStream,
|
||||
fileName,
|
||||
contentType,
|
||||
fileSize
|
||||
);
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error(
|
||||
"Failed to upload file for FileAsset {}:", assetPath
|
||||
|
|
@ -414,4 +423,18 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
return headers.getFirst("Content-Type");
|
||||
}
|
||||
|
||||
private long getFileSize(final MultivaluedMap<String, String> headers) {
|
||||
if (headers.containsKey("Content-Length")) {
|
||||
try {
|
||||
return Long.parseLong(
|
||||
headers.getFirst("Content-Length")
|
||||
);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new UnexpectedErrorException(ex);
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue