use of com.pspace.ifs.ksan.gw.data.DataUploadPart in project ksan by infinistor.
the class UploadPart method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_UPLOAD_PART_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
String object = s3Parameter.getObjectName();
S3Bucket s3Bucket = new S3Bucket();
s3Bucket.setCors(getBucketInfo().getCors());
s3Bucket.setAccess(getBucketInfo().getAccess());
s3Parameter.setBucket(s3Bucket);
GWUtils.checkCors(s3Parameter);
if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
DataUploadPart dataUploadPart = new DataUploadPart(s3Parameter);
dataUploadPart.extract();
String partNumberStr = dataUploadPart.getPartNumber();
int partNumber = Integer.parseInt(partNumberStr);
String uploadId = dataUploadPart.getUploadId();
s3Parameter.setUploadId(uploadId);
s3Parameter.setPartNumber(partNumberStr);
if (partNumber < 1 || partNumber > GWConstants.MAX_PARTS_SIZE) {
logger.error(GWErrorCode.INVALID_ARGUMENT.getMessage() + GWConstants.LOG_UPLOAD_PART_WRONG_PART_NUMBER);
throw new GWException(GWErrorCode.INVALID_ARGUMENT, GWConstants.LOG_UPLOAD_PART_WRONG_PART_NUMBER, (Throwable) null, ImmutableMap.of(GWConstants.ARGMENT_NAME, GWConstants.PART_NUMBER, GWConstants.ARGMENT_VALUE, partNumberStr), s3Parameter);
}
String contentLength = dataUploadPart.getContentLength();
String contentMD5String = dataUploadPart.getContentMD5();
String customerAlgorithm = dataUploadPart.getServerSideEncryptionCustomerAlgorithm();
String customerKey = dataUploadPart.getServerSideEncryptionCustomerKey();
String customerKeyMD5 = dataUploadPart.getServerSideEncryptionCustomerKeyMD5();
if (Strings.isNullOrEmpty(contentLength)) {
logger.error(GWConstants.LENGTH_REQUIRED);
throw new GWException(GWErrorCode.MISSING_CONTENT_LENGTH, s3Parameter);
}
// get metadata
S3Metadata s3Metadata = new S3Metadata();
ObjMultipart objMultipart = null;
Multipart multipart = null;
try {
objMultipart = new ObjMultipart(bucket);
multipart = objMultipart.getMultipart(uploadId);
if (multipart == null) {
logger.error(GWConstants.LOG_UPLOAD_NOT_FOUND, uploadId);
throw new GWException(GWErrorCode.NO_SUCH_UPLOAD, s3Parameter);
}
} catch (UnknownHostException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
} catch (Exception e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
ObjectMapper jsonMapper = new ObjectMapper();
try {
s3Metadata = jsonMapper.readValue(multipart.getMeta(), S3Metadata.class);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
}
Metadata objMeta = createLocal(bucket, object);
if (!Strings.isNullOrEmpty(contentMD5String)) {
s3Metadata.setContentMD5(contentMD5String);
}
long length = Long.parseLong(contentLength);
s3Metadata.setContentLength(length);
String path = GWDiskConfig.getInstance().getLocalPath();
S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, null, null);
Metadata part = null;
S3Object s3Object = null;
try {
part = objMultipart.getObjectWithUploadIdPartNo(uploadId, partNumber);
if (part != null) {
objectOperation.deletePart(part.getPrimaryDisk().getId());
}
s3Object = objectOperation.uploadPart(path, length);
} catch (Exception e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
}
objMultipart.startSingleUpload(object, uploadId, partNumber, "", "", s3Object.getEtag(), s3Object.getFileSize(), objMeta.getPrimaryDisk().getId());
objMultipart.finishSingleUpload(uploadId, partNumber);
s3Parameter.addRequestSize(s3Object.getFileSize());
s3Parameter.setFileSize(s3Object.getFileSize());
s3Parameter.getResponse().addHeader(HttpHeaders.ETAG, GWUtils.maybeQuoteETag(s3Object.getEtag()));
}
Aggregations