Moved logic for saving asset data to separate class.
parent
100f6f45f8
commit
f022a9540d
|
|
@ -21,7 +21,6 @@ package org.librecms.assets;
|
||||||
import org.librecms.contentsection.Asset;
|
import org.librecms.contentsection.Asset;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.activation.MimeType;
|
import javax.activation.MimeType;
|
||||||
|
|
@ -48,6 +47,7 @@ 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;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import static org.librecms.CmsConstants.*;
|
import static org.librecms.CmsConstants.*;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,17 @@
|
||||||
*/
|
*/
|
||||||
package org.librecms.assets;
|
package org.librecms.assets;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.hibernate.engine.jdbc.BlobProxy;
|
||||||
import org.libreccm.core.UnexpectedErrorException;
|
import org.libreccm.core.UnexpectedErrorException;
|
||||||
|
import org.librecms.contentsection.AssetRepository;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.sql.Blob;
|
import java.sql.Blob;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
|
|
@ -30,9 +36,12 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.activation.MimeType;
|
||||||
|
import javax.activation.MimeTypeParseException;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.enterprise.context.ApplicationScoped;
|
|
||||||
import javax.enterprise.context.Dependent;
|
import javax.enterprise.context.Dependent;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
|
@ -43,14 +52,23 @@ import javax.transaction.Transactional;
|
||||||
@Dependent
|
@Dependent
|
||||||
public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LogManager.getLogger(
|
||||||
|
BinaryAssetBlobDataProvider.class
|
||||||
|
);
|
||||||
|
|
||||||
@Resource(lookup = "java:/comp/env/jdbc/libreccm/db")
|
@Resource(lookup = "java:/comp/env/jdbc/libreccm/db")
|
||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private AssetRepository assetRepo;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copyDataToOutputStream(
|
public void copyDataToOutputStream(
|
||||||
final BinaryAsset asset, final OutputStream outputStream
|
final BinaryAsset asset, final OutputStream outputStream
|
||||||
) {
|
) {
|
||||||
Objects.requireNonNull(asset, "Can't retrieve data from null.");
|
|
||||||
try ( Connection connection = dataSource.getConnection()) {
|
try ( Connection connection = dataSource.getConnection()) {
|
||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
final PreparedStatement stmt = connection
|
final PreparedStatement stmt = connection
|
||||||
|
|
@ -62,42 +80,115 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
||||||
try ( ResultSet resultSet = stmt.executeQuery()) {
|
try ( ResultSet resultSet = stmt.executeQuery()) {
|
||||||
resultSet.next();
|
resultSet.next();
|
||||||
final Blob blob = resultSet.getBlob("asset_data");
|
final Blob blob = resultSet.getBlob("asset_data");
|
||||||
blob.getBinaryStream().transferTo(outputStream);
|
// blob.getBinaryStream().transferTo(outputStream);
|
||||||
// try ( InputStream inputStream = blob.getBinaryStream()) {
|
try ( InputStream inputStream = blob.getBinaryStream()) {
|
||||||
// byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
// int length;
|
int length;
|
||||||
// while ((length = inputStream.read(buffer)) != -1) {
|
while ((length = inputStream.read(buffer)) != -1) {
|
||||||
// outputStream.write(buffer, 0, length);
|
outputStream.write(buffer, 0, length);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException | IOException ex) {
|
} catch (SQLException | IOException ex) {
|
||||||
throw new UnexpectedErrorException(ex);
|
throw new UnexpectedErrorException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
@Override
|
@Override
|
||||||
public void saveData(
|
public void saveData(
|
||||||
final BinaryAsset asset,
|
final BinaryAsset asset,
|
||||||
final InputStream stream,
|
final InputStream inputStream,
|
||||||
final String fileName,
|
final String fileName,
|
||||||
final String mimeType,
|
final String mimeType,
|
||||||
final long fileSize
|
final long fileSizeParam
|
||||||
) {
|
) {
|
||||||
Objects.requireNonNull(asset, "Can't save data to null.");
|
Objects.requireNonNull(asset, "Can't save data to null.");
|
||||||
try ( Connection connection = dataSource.getConnection()) {
|
Objects.requireNonNull(inputStream, "Can't read data from null");
|
||||||
final PreparedStatement stmt = connection
|
Objects.requireNonNull(fileName);
|
||||||
.prepareStatement(
|
Objects.requireNonNull(mimeType);
|
||||||
"UPDATE ccm_cms.binary_assets SET asset_data = ?, filename = ?, mime_type = ?, data_size = ? WHERE object_id = ?"
|
|
||||||
|
try {
|
||||||
|
final Path tmpFilePath = Files.createTempFile("upload", fileName);
|
||||||
|
int fileSize = 0;
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
final Blob data = BlobProxy.generateProxy(
|
||||||
|
Files.newInputStream(tmpFilePath), -1
|
||||||
);
|
);
|
||||||
stmt.setBlob(1, stream);
|
asset.setFileName(fileName);
|
||||||
stmt.setString(2, fileName);
|
asset.setData(data);
|
||||||
stmt.setString(3, mimeType);
|
asset.setSize(fileSize);
|
||||||
stmt.setLong(4, fileSize);
|
try {
|
||||||
stmt.setLong(5, asset.getObjectId());
|
asset.setMimeType(new MimeType(mimeType));
|
||||||
|
} catch (MimeTypeParseException ex) {
|
||||||
|
LOGGER.error(
|
||||||
|
"Failed to upload file for FileAsset {}:",
|
||||||
|
asset.getUuid()
|
||||||
|
);
|
||||||
|
LOGGER.error(ex);
|
||||||
|
|
||||||
stmt.execute();
|
throw new UnexpectedErrorException(ex);
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new UnexpectedErrorException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
assetRepo.save(asset);
|
||||||
|
entityManager.flush();
|
||||||
|
|
||||||
|
updateAudTable(asset.getObjectId());
|
||||||
|
|
||||||
|
// try ( Connection connection = dataSource.getConnection()) {
|
||||||
|
// final PreparedStatement stmt = connection
|
||||||
|
// .prepareStatement(
|
||||||
|
// "UPDATE ccm_cms.binary_assets SET asset_data = ?, filename = ?, mime_type = ?, data_size = ? WHERE object_id = ?"
|
||||||
|
// );
|
||||||
|
// stmt.setBlob(1, stream);
|
||||||
|
// 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);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
private void updateAudTable(final long assetId) {
|
||||||
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
|
final PreparedStatement findRevStmt = connection
|
||||||
|
.prepareStatement(
|
||||||
|
"SELECT rev FROM ccm_cms.binary_assets_aud WHERE object_id = ? ORDER BY rev DESC LIMIT 1"
|
||||||
|
);
|
||||||
|
findRevStmt.setLong(1, assetId);
|
||||||
|
|
||||||
|
final long rev;
|
||||||
|
try(ResultSet resultSet = findRevStmt.executeQuery()) {
|
||||||
|
resultSet.next();
|
||||||
|
rev = resultSet.getLong("rev");
|
||||||
|
}
|
||||||
|
|
||||||
|
final PreparedStatement updateDataStmt = connection
|
||||||
|
.prepareStatement(
|
||||||
|
"UPDATE ccm_cms.binary_assets_aud SET asset_data = (SELECT asset_data FROM ccm_cms.binary_assets WHERE object_id = ?) WHERE object_id = ? AND rev = ?"
|
||||||
|
);
|
||||||
|
updateDataStmt.setLong(1, assetId);
|
||||||
|
updateDataStmt.setLong(2, assetId);
|
||||||
|
updateDataStmt.setLong(3, rev);
|
||||||
|
|
||||||
|
updateDataStmt.execute();
|
||||||
} catch(SQLException ex) {
|
} catch(SQLException ex) {
|
||||||
throw new UnexpectedErrorException(ex);
|
throw new UnexpectedErrorException(ex);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,6 @@ 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.BufferedInputStream;
|
|
||||||
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.io.OutputStream;
|
||||||
|
|
@ -355,65 +353,60 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
||||||
try {
|
try {
|
||||||
final MultivaluedMap<String, String> headers = inputPart
|
final MultivaluedMap<String, String> headers = inputPart
|
||||||
.getHeaders();
|
.getHeaders();
|
||||||
|
|
||||||
fileName = getFileName(headers);
|
fileName = getFileName(headers);
|
||||||
contentType = getContentType(headers);
|
contentType = getContentType(headers);
|
||||||
// fileSize = getFileSize(headers);
|
|
||||||
|
|
||||||
final java.nio.file.Path tmpFilePath = Files
|
dataService.saveData(
|
||||||
.createTempFile(
|
fileAsset,
|
||||||
"upload", fileName
|
inputPart.getBody(InputStream.class, null),
|
||||||
|
fileName,
|
||||||
|
contentType,
|
||||||
|
fileSize
|
||||||
);
|
);
|
||||||
try ( InputStream inputStream = inputPart.getBody(
|
//
|
||||||
InputStream.class, null
|
// final java.nio.file.Path tmpFilePath = Files
|
||||||
)) {
|
// .createTempFile(
|
||||||
try ( OutputStream outputStream = Files.newOutputStream(
|
// "upload", fileName
|
||||||
tmpFilePath
|
|
||||||
)) {
|
|
||||||
int length;
|
|
||||||
byte[] buffer = new byte[8192];
|
|
||||||
while ((length = inputStream.read(buffer)) != -1) {
|
|
||||||
outputStream.write(buffer);
|
|
||||||
fileSize += length;
|
|
||||||
}
|
|
||||||
outputStream.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
// );
|
// );
|
||||||
|
// 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();
|
||||||
} catch (IOException ex) {
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// final Blob data = BlobProxy.generateProxy(
|
||||||
|
// Files.newInputStream(tmpFilePath), -1
|
||||||
|
// );
|
||||||
|
// fileAsset.setFileName(fileName);
|
||||||
|
// fileAsset.setData(data);
|
||||||
|
//
|
||||||
|
// fileAsset.setSize(fileSize);
|
||||||
|
// 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);
|
||||||
|
} catch (IOException | UnexpectedErrorException ex) {
|
||||||
LOGGER.error(
|
LOGGER.error(
|
||||||
"Failed to upload file for FileAsset {}:", assetPath
|
"Failed to upload file for FileAsset {}:", assetPath
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -24,20 +24,13 @@ import org.librecms.assets.FileAsset;
|
||||||
import org.librecms.contentsection.Asset;
|
import org.librecms.contentsection.Asset;
|
||||||
import org.librecms.contentsection.AssetRepository;
|
import org.librecms.contentsection.AssetRepository;
|
||||||
import org.librecms.contentsection.ContentSection;
|
import org.librecms.contentsection.ContentSection;
|
||||||
import org.librecms.ui.contentsections.ContentSectionNotFoundException;
|
|
||||||
import org.librecms.ui.contentsections.ContentSectionsUi;
|
import org.librecms.ui.contentsections.ContentSectionsUi;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.mail.internet.ContentDisposition;
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
|
@ -67,14 +60,12 @@ public class FileAssetEditStepDownload {
|
||||||
@GET
|
@GET
|
||||||
@Path("/")
|
@Path("/")
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
|
||||||
public Response downloadFile(
|
public Response downloadFile(
|
||||||
@PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM)
|
@PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM)
|
||||||
final String sectionIdentifier,
|
final String sectionIdentifier,
|
||||||
@PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME)
|
@PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME)
|
||||||
final String assetPath
|
final String assetPath
|
||||||
) {
|
) {
|
||||||
|
|
||||||
final ContentSection contentSection = sectionsUi
|
final ContentSection contentSection = sectionsUi
|
||||||
.findContentSection(sectionIdentifier)
|
.findContentSection(sectionIdentifier)
|
||||||
.orElseThrow(
|
.orElseThrow(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue