Bugfixes for BinaryAssetDataService
parent
c8382b6660
commit
de65988459
|
|
@ -20,6 +20,7 @@ package org.librecms.assets;
|
|||
|
||||
import org.libreccm.core.UnexpectedErrorException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.sql.Blob;
|
||||
|
|
@ -45,20 +46,30 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
|||
private DataSource dataSource;
|
||||
|
||||
@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.");
|
||||
try ( Connection connection = dataSource.getConnection()) {
|
||||
connection.setAutoCommit(false);
|
||||
final PreparedStatement stmt = connection
|
||||
.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());
|
||||
|
||||
try (ResultSet resultSet = stmt.executeQuery()) {
|
||||
final Blob blob = resultSet.getBlob("data");
|
||||
return blob.getBinaryStream();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
} catch (SQLException |IOException ex) {
|
||||
throw new UnexpectedErrorException(ex);
|
||||
}
|
||||
}
|
||||
|
|
@ -69,10 +80,11 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
|
|||
try ( Connection connection = dataSource.getConnection()) {
|
||||
final PreparedStatement stmt = connection
|
||||
.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.setLong(2, asset.getObjectId());
|
||||
// connection.commit();
|
||||
} catch (SQLException ex) {
|
||||
throw new UnexpectedErrorException(ex);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
package org.librecms.assets;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -26,7 +27,9 @@ import java.io.InputStream;
|
|||
*/
|
||||
public interface BinaryAssetDataProvider {
|
||||
|
||||
InputStream retrieveData(BinaryAsset asset);
|
||||
void copyDataToOutputStream(
|
||||
BinaryAsset asset, OutputStream outputStream
|
||||
);
|
||||
|
||||
void saveData(BinaryAsset asset, InputStream stream);
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import org.libreccm.configuration.ConfigurationManager;
|
|||
import org.libreccm.core.UnexpectedErrorException;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
|
|
@ -43,12 +44,12 @@ public class BinaryAssetDataService {
|
|||
@Any
|
||||
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.");
|
||||
|
||||
final BinaryAssetDataProvider dataProvider = getDataProvider();
|
||||
|
||||
return dataProvider.retrieveData(asset);
|
||||
Objects.requireNonNull(outputStream, "Can't copy data to null.");
|
||||
getDataProvider().copyDataToOutputStream(asset, outputStream);
|
||||
}
|
||||
|
||||
public void saveData(final BinaryAsset asset, final InputStream stream) {
|
||||
|
|
|
|||
|
|
@ -316,9 +316,6 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@POST
|
||||
@Path("/upload")
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
|
|
@ -346,7 +343,6 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
.getFormDataMap();
|
||||
final List<InputPart> inputParts = uploadForm.get("fileData");
|
||||
|
||||
final Part part;
|
||||
String fileName = "";
|
||||
String contentType = "";
|
||||
for (final InputPart inputPart : inputParts) {
|
||||
|
|
@ -355,10 +351,27 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
.getHeaders();
|
||||
fileName = getFileName(headers);
|
||||
contentType = getContentType(headers);
|
||||
dataService.saveData(
|
||||
fileAsset,
|
||||
inputPart.getBody(InputStream.class, null)
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error(
|
||||
"Failed to upload file for FileAsset {}:", assetPath
|
||||
|
|
@ -368,51 +381,8 @@ public class FileAssetEditStep extends AbstractMvcAssetEditStep {
|
|||
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.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();
|
||||
} else {
|
||||
return assetUi.showAccessDenied(
|
||||
|
|
|
|||
|
|
@ -122,50 +122,27 @@ public class FileAssetEditStepDownload {
|
|||
);
|
||||
}
|
||||
final FileAsset fileAsset = (FileAsset) asset;
|
||||
try ( InputStream dataInputStream = dataService.retrieveData(fileAsset)) {
|
||||
|
||||
final StreamingOutput output = new StreamingOutput() {
|
||||
return Response
|
||||
.ok()
|
||||
.entity(
|
||||
new StreamingOutput() {
|
||||
|
||||
@Override
|
||||
public void write(final OutputStream outputStream)
|
||||
throws IOException, WebApplicationException {
|
||||
byte[] buffer = new byte[8192];
|
||||
int length;
|
||||
while ((length = dataInputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, length);
|
||||
}
|
||||
dataService.copyDataToOutputStream(fileAsset, outputStream);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return Response
|
||||
.ok()
|
||||
.entity(output)
|
||||
.header("Content-Type", fileAsset.getMimeType())
|
||||
.header(
|
||||
"Content-Disposition",
|
||||
String.format(
|
||||
"attachment; filename=\"%s\"",
|
||||
fileAsset.getFileName()
|
||||
)
|
||||
})
|
||||
.header("Content-Type", fileAsset.getMimeType())
|
||||
.header(
|
||||
"Content-Disposition",
|
||||
String.format(
|
||||
"attachment; filename=\"%s\"",
|
||||
fileAsset.getFileName()
|
||||
)
|
||||
.build();
|
||||
|
||||
} catch (IOException ex) {
|
||||
throw new WebApplicationException(
|
||||
ex,
|
||||
Response.Status.INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
// private class FileAssetOutput implements StreamingOutput {
|
||||
//
|
||||
// @Override
|
||||
// public void write(final OutputStream outputStream)
|
||||
// throws IOException, WebApplicationException {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue