mirror of https://github.com/apache/cloudstack.git
Added file exists check for onetime post url
also, fixed an issue where in the upload was going to error state in case of parallel call to the same post url
This commit is contained in:
parent
4f35d3611a
commit
a7e511c1a3
|
|
@ -77,7 +77,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
|
||||
private String uuid;
|
||||
|
||||
private boolean fileReceived = false;
|
||||
private boolean requestProcessed = false;
|
||||
|
||||
private static final String HEADER_SIGNATURE = "X-signature";
|
||||
|
||||
|
|
@ -96,12 +96,12 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
if (decoder != null) {
|
||||
decoder.cleanFiles();
|
||||
}
|
||||
fileReceived = false;
|
||||
requestProcessed = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
if (!fileReceived) {
|
||||
if (!requestProcessed) {
|
||||
String message = "file receive failed or connection closed prematurely.";
|
||||
logger.error(message);
|
||||
storageResource.updateStateMapWithError(uuid, message);
|
||||
|
|
@ -163,12 +163,14 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
logger.error("post request validation failed", ex);
|
||||
responseContent.append(ex.getMessage());
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
|
||||
requestProcessed = true;
|
||||
return;
|
||||
}
|
||||
if (uploadEntity == null) {
|
||||
logger.error("Unable to create upload entity. An exception occurred.");
|
||||
responseContent.append("Internal Server Error");
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
requestProcessed = true;
|
||||
return;
|
||||
}
|
||||
//set the base directory to download the file
|
||||
|
|
@ -181,12 +183,14 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
logger.error("exception while initialising the decoder", e);
|
||||
responseContent.append(e.getMessage());
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
requestProcessed = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
logger.warn("received a get request");
|
||||
responseContent.append("only post requests are allowed");
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
|
||||
requestProcessed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -202,6 +206,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
logger.error("data decoding exception", e);
|
||||
responseContent.append(e.getMessage());
|
||||
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
requestProcessed = true;
|
||||
return;
|
||||
}
|
||||
if (chunk instanceof LastHttpContent) {
|
||||
|
|
@ -229,7 +234,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
|
|||
if (data.getHttpDataType() == HttpDataType.FileUpload) {
|
||||
FileUpload fileUpload = (FileUpload) data;
|
||||
if (fileUpload.isCompleted()) {
|
||||
fileReceived = true;
|
||||
requestProcessed = true;
|
||||
String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
|
||||
if(StringUtils.isNotBlank(format)) {
|
||||
String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
|
||||
|
|
|
|||
|
|
@ -2617,9 +2617,13 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
|
|||
throw new InvalidParameterValueException("unable to decode and deserialize metadata");
|
||||
} else {
|
||||
uuid = cmd.getEntityUUID();
|
||||
if (uploadEntityStateMap.containsKey(uuid)) {
|
||||
if (isOneTimePostUrlUsed(cmd)) {
|
||||
uploadEntity = uploadEntityStateMap.get(uuid);
|
||||
throw new InvalidParameterValueException("The one time post url is already used and the upload is in " + uploadEntity.getUploadState() + " state.");
|
||||
StringBuilder errorMessage = new StringBuilder("The one time post url is already used");
|
||||
if (uploadEntity != null) {
|
||||
errorMessage.append(" and the upload is in ").append(uploadEntity.getUploadState()).append(" state.");
|
||||
}
|
||||
throw new InvalidParameterValueException(errorMessage.toString());
|
||||
}
|
||||
int maxSizeInGB = Integer.valueOf(cmd.getMaxUploadSize());
|
||||
int contentLengthInGB = getSizeInGB(contentLength);
|
||||
|
|
@ -2657,6 +2661,12 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
|
|||
return uploadEntity;
|
||||
}
|
||||
|
||||
private boolean isOneTimePostUrlUsed(TemplateOrVolumePostUploadCommand cmd) {
|
||||
String uuid = cmd.getEntityUUID();
|
||||
String uploadPath = this.getRootDir(cmd.getDataTo()) + File.separator + cmd.getAbsolutePath();
|
||||
return uploadEntityStateMap.containsKey(uuid) || new File(uploadPath).exists();
|
||||
}
|
||||
|
||||
private int getSizeInGB(long sizeInBytes) {
|
||||
return (int)Math.ceil(sizeInBytes * 1.0d / (1024 * 1024 * 1024));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue