Bugfixes for BinaryAssetDataService
parent
c8382b6660
commit
de65988459
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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,10 +351,27 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
||||||
.getHeaders();
|
.getHeaders();
|
||||||
fileName = getFileName(headers);
|
fileName = getFileName(headers);
|
||||||
contentType = getContentType(headers);
|
contentType = getContentType(headers);
|
||||||
dataService.saveData(
|
|
||||||
fileAsset,
|
fileAsset.setFileName(fileName);
|
||||||
inputPart.getBody(InputStream.class, null)
|
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);
|
||||||
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LOGGER.error(
|
LOGGER.error(
|
||||||
"Failed to upload file for FileAsset {}:", assetPath
|
"Failed to upload file for FileAsset {}:", assetPath
|
||||||
|
|
@ -368,51 +381,8 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
||||||
models.put("uploadFailed", true);
|
models.put("uploadFailed", true);
|
||||||
return buildRedirectPathForStep();
|
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.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);
|
|
||||||
|
|
||||||
return buildRedirectPathForStep();
|
return buildRedirectPathForStep();
|
||||||
} else {
|
} else {
|
||||||
return assetUi.showAccessDenied(
|
return assetUi.showAccessDenied(
|
||||||
|
|
|
||||||
|
|
@ -122,50 +122,27 @@ 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
})
|
||||||
|
.header("Content-Type", fileAsset.getMimeType())
|
||||||
return Response
|
.header(
|
||||||
.ok()
|
"Content-Disposition",
|
||||||
.entity(output)
|
String.format(
|
||||||
.header("Content-Type", fileAsset.getMimeType())
|
"attachment; filename=\"%s\"",
|
||||||
.header(
|
fileAsset.getFileName()
|
||||||
"Content-Disposition",
|
|
||||||
String.format(
|
|
||||||
"attachment; filename=\"%s\"",
|
|
||||||
fileAsset.getFileName()
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.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 {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue