Bugfixes for BinaryAssetDataService

pull/10/head
Jens Pelzetter 2021-06-23 21:06:20 +02:00
parent c8382b6660
commit de65988459
5 changed files with 63 additions and 100 deletions

View File

@ -20,6 +20,7 @@ package org.librecms.assets;
import org.libreccm.core.UnexpectedErrorException; import org.libreccm.core.UnexpectedErrorException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Blob; import java.sql.Blob;
@ -45,20 +46,30 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
private DataSource dataSource; private DataSource dataSource;
@Override @Override
public InputStream retrieveData(final BinaryAsset asset) { public void copyDataToOutputStream(
final BinaryAsset asset, final OutputStream outputStream
) {
Objects.requireNonNull(asset, "Can't retrieve data from null."); Objects.requireNonNull(asset, "Can't retrieve data from null.");
try ( Connection connection = dataSource.getConnection()) { try ( Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(false);
final PreparedStatement stmt = connection final PreparedStatement stmt = connection
.prepareStatement( .prepareStatement(
"SELECT data FROM binary_assets WHERE object_id = ?" "SELECT asset_data FROM ccm_cms.binary_assets WHERE object_id = ?"
); );
stmt.setLong(1, asset.getObjectId()); stmt.setLong(1, asset.getObjectId());
try (ResultSet resultSet = stmt.executeQuery()) { try (ResultSet resultSet = stmt.executeQuery()) {
final Blob blob = resultSet.getBlob("data"); resultSet.next();
return blob.getBinaryStream(); 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);
} }
} catch (SQLException ex) { }
}
} catch (SQLException |IOException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
} }
@ -69,10 +80,11 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
try ( Connection connection = dataSource.getConnection()) { try ( Connection connection = dataSource.getConnection()) {
final PreparedStatement stmt = connection final PreparedStatement stmt = connection
.prepareStatement( .prepareStatement(
"UPDATE binary_assets SET data = ? WHERE object_id = ?" "UPDATE ccm_cms.binary_assets SET asset_data = ? WHERE object_id = ?"
); );
stmt.setBlob(1, stream); stmt.setBlob(1, stream);
stmt.setLong(2, asset.getObjectId()); stmt.setLong(2, asset.getObjectId());
// connection.commit();
} catch (SQLException ex) { } catch (SQLException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }

View File

@ -19,6 +19,7 @@
package org.librecms.assets; package org.librecms.assets;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
/** /**
* *
@ -26,7 +27,9 @@ import java.io.InputStream;
*/ */
public interface BinaryAssetDataProvider { public interface BinaryAssetDataProvider {
InputStream retrieveData(BinaryAsset asset); void copyDataToOutputStream(
BinaryAsset asset, OutputStream outputStream
);
void saveData(BinaryAsset asset, InputStream stream); void saveData(BinaryAsset asset, InputStream stream);

View File

@ -22,6 +22,7 @@ import org.libreccm.configuration.ConfigurationManager;
import org.libreccm.core.UnexpectedErrorException; import org.libreccm.core.UnexpectedErrorException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects; import java.util.Objects;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -43,12 +44,12 @@ public class BinaryAssetDataService {
@Any @Any
private Instance<BinaryAssetDataProvider> dataProvider; private Instance<BinaryAssetDataProvider> dataProvider;
public InputStream retrieveData(final BinaryAsset asset) { public void copyDataToOutputStream(
final BinaryAsset asset, final OutputStream outputStream
) {
Objects.requireNonNull(asset, "Can't retrieve data from null."); Objects.requireNonNull(asset, "Can't retrieve data from null.");
Objects.requireNonNull(outputStream, "Can't copy data to null.");
final BinaryAssetDataProvider dataProvider = getDataProvider(); getDataProvider().copyDataToOutputStream(asset, outputStream);
return dataProvider.retrieveData(asset);
} }
public void saveData(final BinaryAsset asset, final InputStream stream) { public void saveData(final BinaryAsset asset, final InputStream stream) {

View File

@ -316,9 +316,6 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
} }
} }
@POST @POST
@Path("/upload") @Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA) @Consumes(MediaType.MULTIPART_FORM_DATA)
@ -346,7 +343,6 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
.getFormDataMap(); .getFormDataMap();
final List<InputPart> inputParts = uploadForm.get("fileData"); final List<InputPart> inputParts = uploadForm.get("fileData");
final Part part;
String fileName = ""; String fileName = "";
String contentType = ""; String contentType = "";
for (final InputPart inputPart : inputParts) { for (final InputPart inputPart : inputParts) {
@ -355,47 +351,6 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
.getHeaders(); .getHeaders();
fileName = getFileName(headers); fileName = getFileName(headers);
contentType = getContentType(headers); contentType = getContentType(headers);
dataService.saveData(
fileAsset,
inputPart.getBody(InputStream.class, null)
);
} catch (IOException ex) {
LOGGER.error(
"Failed to upload file for FileAsset {}:", assetPath
);
LOGGER.error(ex);
models.put("uploadFailed", true);
return buildRedirectPathForStep();
}
// final MultivaluedMap<String, String> headers = inputPart
// .getHeaders();
// fileName = getFileName(headers);
// contentType = getContentType(headers);
// final byte[] bytes = new byte[1024];
// try (InputStream inputStream = inputPart.getBody(
// InputStream.class, null
// );
// ByteArrayOutputStream fileDataOutputStream
// = new ByteArrayOutputStream()) {
// while (inputStream.read(bytes) != -1) {
// fileDataOutputStream.writeBytes(bytes);
// }
//
// fileAsset.setData(fileDataOutputStream.toByteArray());
// }
//
// } catch (IOException ex) {
// LOGGER.error(
// "Failed to upload file for FileAsset {}:", assetPath
// );
// LOGGER.error(ex);
//
// models.put("uploadFailed", true);
// return buildRedirectPathForStep();
// }
}
fileAsset.setFileName(fileName); fileAsset.setFileName(fileName);
fileAsset.setSize(fileAsset.getData().length); fileAsset.setSize(fileAsset.getData().length);
@ -413,6 +368,21 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
assetRepo.save(fileAsset); assetRepo.save(fileAsset);
try ( InputStream inputStream = inputPart.getBody(
InputStream.class, null)) {
dataService.saveData(fileAsset, inputStream);
}
} catch (IOException ex) {
LOGGER.error(
"Failed to upload file for FileAsset {}:", assetPath
);
LOGGER.error(ex);
models.put("uploadFailed", true);
return buildRedirectPathForStep();
}
}
return buildRedirectPathForStep(); return buildRedirectPathForStep();
} else { } else {
return assetUi.showAccessDenied( return assetUi.showAccessDenied(

View File

@ -122,25 +122,18 @@ public class FileAssetEditStepDownload {
); );
} }
final FileAsset fileAsset = (FileAsset) asset; final FileAsset fileAsset = (FileAsset) asset;
try ( InputStream dataInputStream = dataService.retrieveData(fileAsset)) { return Response
.ok()
final StreamingOutput output = new StreamingOutput() { .entity(
new StreamingOutput() {
@Override @Override
public void write(final OutputStream outputStream) public void write(final OutputStream outputStream)
throws IOException, WebApplicationException { throws IOException, WebApplicationException {
byte[] buffer = new byte[8192]; dataService.copyDataToOutputStream(fileAsset, outputStream);
int length;
while ((length = dataInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
} }
}; })
return Response
.ok()
.entity(output)
.header("Content-Type", fileAsset.getMimeType()) .header("Content-Type", fileAsset.getMimeType())
.header( .header(
"Content-Disposition", "Content-Disposition",
@ -150,22 +143,6 @@ public class FileAssetEditStepDownload {
) )
) )
.build(); .build();
} catch (IOException ex) {
throw new WebApplicationException(
ex,
Response.Status.INTERNAL_SERVER_ERROR
);
}
} }
// private class FileAssetOutput implements StreamingOutput {
//
// @Override
// public void write(final OutputStream outputStream)
// throws IOException, WebApplicationException {
//
// }
//
// }
} }