Hashtable<String, InputStream> inputFiles, byte[] aesKey)
throws Exception {
AccessPolicyInfo accessPolicy = service.create(AccessPolicy.create(
accessPolicyPrefix + "tempAccessPolicy", uploadWindowInMinutes,
EnumSet.of(AccessPolicyPermission.WRITE)));
LocatorInfo locator = service.create(Locator.create(
accessPolicy.getId(), asset.getId(), LocatorType.SAS));
String contentKeyId = createAssetContentKey(asset, aesKey);
WritableBlobContainerContract uploader = service
.createBlobWriter(locator);
Hashtable<String, AssetFileInfo> infoToUpload = new Hashtable<String, AssetFileInfo>();
boolean isFirst = true;
for (String fileName : inputFiles.keySet()) {
MessageDigest digest = MessageDigest.getInstance("MD5");
InputStream inputStream = inputFiles.get(fileName);
byte[] iv = null;
if (aesKey != null) {
iv = createIV();
inputStream = EncryptionHelper.encryptFile(inputStream, aesKey,
iv);
}
InputStream digestStream = new DigestInputStream(inputStream,
digest);
CountingStream countingStream = new CountingStream(digestStream);
uploader.createBlockBlob(fileName, countingStream);
inputStream.close();
byte[] md5hash = digest.digest();
String md5 = Base64.encode(md5hash);
AssetFileInfo fi = new AssetFileInfo(null, new AssetFileType()
.setContentChecksum(md5)
.setContentFileSize(countingStream.getCount())
.setIsPrimary(isFirst).setName(fileName)
.setInitializationVector(getIVString(iv)));
infoToUpload.put(fileName, fi);
isFirst = false;
}
service.action(AssetFile.createFileInfos(asset.getId()));
for (AssetFileInfo assetFile : service.list(AssetFile.list(asset
.getAssetFilesLink()))) {
AssetFileInfo fileInfo = infoToUpload.get(assetFile.getName());
Updater updateOp = AssetFile.update(assetFile.getId())
.setContentChecksum(fileInfo.getContentChecksum())
.setContentFileSize(fileInfo.getContentFileSize())
.setIsPrimary(fileInfo.getIsPrimary());
if (aesKey != null) {
updateOp.setIsEncrypted(true)
.setEncryptionKeyId(contentKeyId)
.setEncryptionScheme("StorageEncryption")
.setEncryptionVersion("1.0")
.setInitializationVector(
fileInfo.getInitializationVector());
}
service.update(updateOp);
}
service.list(AssetFile.list(asset.getAssetFilesLink()));
service.delete(Locator.delete(locator.getId()));
service.delete(AccessPolicy.delete(accessPolicy.getId()));
}