mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
volume upload: added md5 checksum validation
also fixed the issue wherein the successful uploads where also moving to error state as the channelinactive is called after the end of successful upload as well. added a fileuploaded boolean to check when the channel is inactive.
This commit is contained in:
parent
6b8b4b92e6
commit
d5dffb5dc9
@ -333,7 +333,7 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
|
||||
* encoded metadata using the post upload config key
|
||||
*/
|
||||
TemplateOrVolumePostUploadCommand command =
|
||||
new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), volumeStore.getChecksum(), vol.getType().toString(),
|
||||
new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), cmd.getChecksum(), vol.getType().toString(),
|
||||
vol.getName(), vol.getFormat().toString(), dataObject.getDataStore().getUri(),
|
||||
dataObject.getDataStore().getRole().toString());
|
||||
command.setLocalPath(volumeStore.getLocalDownloadPath());
|
||||
|
||||
@ -75,6 +75,8 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
|
||||
private String uuid;
|
||||
|
||||
private boolean fileReceived = false;
|
||||
|
||||
private static final String HEADER_SIGNATURE = "X-signature";
|
||||
|
||||
private static final String HEADER_METADATA = "X-metadata";
|
||||
@ -92,13 +94,16 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
if (decoder != null) {
|
||||
decoder.cleanFiles();
|
||||
}
|
||||
fileReceived = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String message = "file receive failed or connection closed prematurely.";
|
||||
logger.error(message);
|
||||
storageResource.updateStateMapWithError(uuid, message);
|
||||
if (!fileReceived) {
|
||||
String message = "file receive failed or connection closed prematurely.";
|
||||
logger.error(message);
|
||||
storageResource.updateStateMapWithError(uuid, message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -198,15 +203,8 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
return;
|
||||
}
|
||||
if (chunk instanceof LastHttpContent) {
|
||||
try {
|
||||
readFileUploadData();
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.OK);
|
||||
reset();
|
||||
} catch (InvalidParameterValueException e) {
|
||||
logger.error("error during the file install.", e);
|
||||
responseContent.append("\n").append(e.getMessage());
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
writeResponse(ctx.channel(), readFileUploadData());
|
||||
reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -220,7 +218,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
decoder = null;
|
||||
}
|
||||
|
||||
private void readFileUploadData() throws IOException {
|
||||
private HttpResponseStatus readFileUploadData() throws IOException {
|
||||
while (decoder.hasNext()) {
|
||||
InterfaceHttpData data = decoder.next();
|
||||
if (data != null) {
|
||||
@ -229,8 +227,16 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
if (data.getHttpDataType() == HttpDataType.FileUpload) {
|
||||
FileUpload fileUpload = (FileUpload) data;
|
||||
if (fileUpload.isCompleted()) {
|
||||
responseContent.append("upload successful.");
|
||||
storageResource.postUpload(uuid, fileUpload.getFile().getName());
|
||||
fileReceived = true;
|
||||
String status = storageResource.postUpload(uuid, fileUpload.getFile().getName());
|
||||
if (status != null) {
|
||||
responseContent.append(status);
|
||||
storageResource.updateStateMapWithError(uuid, status);
|
||||
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
|
||||
} else {
|
||||
responseContent.append("upload successful.");
|
||||
return HttpResponseStatus.OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@ -238,6 +244,8 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
||||
}
|
||||
}
|
||||
}
|
||||
responseContent.append("received entity is not a file");
|
||||
return HttpResponseStatus.UNPROCESSABLE_ENTITY;
|
||||
}
|
||||
|
||||
private void writeResponse(Channel channel, HttpResponseStatus statusCode) {
|
||||
|
||||
@ -2662,16 +2662,13 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
|
||||
UploadEntity.ResourceType resourceType = uploadEntity.getResourceType();
|
||||
|
||||
String fileSavedTempLocation = uploadEntity.getInstallPathPrefix() + "/" + filename;
|
||||
//String checkSum = computeCheckSum(originalTemplate);
|
||||
//if (checkSum == null) {
|
||||
// s_logger.warn("Something wrong happened when try to calculate the checksum of downloaded template!");
|
||||
//}
|
||||
//dnld.setCheckSum(checkSum);
|
||||
|
||||
int imgSizeGigs = getSizeInGB(_storage.getSize(fileSavedTempLocation));
|
||||
int maxSize = uploadEntity.getMaxSizeInGB();
|
||||
if(imgSizeGigs > maxSize) {
|
||||
throw new InvalidParameterValueException("Maximum file upload size exceeded. Physical file size: "+imgSizeGigs+"GB. Maximum allowed size: "+maxSize+"GB.");
|
||||
String errorMessage = "Maximum file upload size exceeded. Physical file size: " + imgSizeGigs + "GB. Maximum allowed size: " + maxSize + "GB.";
|
||||
s_logger.error(errorMessage);
|
||||
return errorMessage;
|
||||
}
|
||||
imgSizeGigs++; // add one just in case
|
||||
long timeout = (long)imgSizeGigs * installTimeoutPerGig;
|
||||
@ -2684,6 +2681,10 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
|
||||
if (uploadEntity.isHvm()) {
|
||||
scr.add("-h");
|
||||
}
|
||||
String checkSum = uploadEntity.getChksum();
|
||||
if (StringUtils.isNotBlank(checkSum)) {
|
||||
scr.add("-c", checkSum);
|
||||
}
|
||||
|
||||
// add options common to ISO and template
|
||||
String extension = uploadEntity.getFormat().getFileExtension();
|
||||
@ -2734,7 +2735,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
|
||||
} catch (IOException e) {
|
||||
s_logger.warn("Something is wrong with template location " + resourcePath, e);
|
||||
loc.purge();
|
||||
return "Unable to download due to " + e.getMessage();
|
||||
return "Unable to upload due to " + e.getMessage();
|
||||
}
|
||||
|
||||
Map<String, Processor> processors = _dlMgr.getProcessors();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user