public override async Task<UploadResult> Upload(string name, string originalName, string mimeType, string description, byte[] data,
IProgress<AsyncProgressEntry> progress, CancellationToken cancellation) {
await Prepare(cancellation);
if (_authToken == null) {
throw new Exception(ToolsStrings.Uploader_AuthenticationTokenIsMissing);
}
var entry = await Request.PostMultipart<InsertResult>(@"https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart",
new InsertParams {
Title = name,
OriginalFilename = originalName,
Description = description,
MimeType = mimeType,
ParentIds = DestinationDirectoryId == null ? null : new[] {
new InsertParamsParent { Id = DestinationDirectoryId }
}
},
_authToken.AccessToken,
data,
mimeType,
progress,
cancellation);
if (entry == null) {
throw new InformativeException(ToolsStrings.Uploader_CannotUploadToGoogleDrive, ToolsStrings.Common_MakeSureThereIsEnoughSpace);
}
var shared = await Request.Post<PermissionResult>($"https://www.googleapis.com/drive/v2/files/fileId/permissions?fileId={entry.Id}",
JsonConvert.SerializeObject(new {
role = @"reader",
type = @"anyone"
}).GetBytes(), _authToken.AccessToken, cancellation);
if (shared == null) {
throw new Exception(ToolsStrings.Uploader_CannotShareGoogleDrive);
}
return new UploadResult {
Id = $"RG{entry.Id}",
DirectUrl = $"https://drive.google.com/uc?export=download&id={entry.Id}"
};
}
}