Merge pull request #3910 from matthewburnett/GoogleCloudStorage-Patch

Google based uploaders update
This commit is contained in:
Jaex 2019-01-27 18:50:01 +03:00 committed by GitHub
commit be2d7e3430
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 40 deletions

View file

@ -78,7 +78,7 @@ public GoogleCloudStorage(OAuth2Info oauth)
{
googleAuth = new GoogleOAuth2(oauth, this)
{
Scope = "https://www.googleapis.com/auth/devstorage.full_control"
Scope = "https://www.googleapis.com/auth/devstorage.read_write"
};
}
@ -106,20 +106,13 @@ public override UploadResult Upload(Stream stream, string fileName)
{
if (!CheckAuthorization()) return null;
string name = fileName;
string uploadPath = GetUploadPath(fileName);
if ((RemoveExtensionImage && Helpers.IsImageFile(fileName)) ||
(RemoveExtensionText && Helpers.IsTextFile(fileName)) ||
(RemoveExtensionVideo && Helpers.IsVideoFile(fileName)))
OnEarlyURLCopyRequested(GenerateURL(uploadPath));
GoogleCloudStorageMetadata googleCloudStorageMetadata = new GoogleCloudStorageMetadata
{
name = Path.GetFileNameWithoutExtension(fileName);
}
string uploadpath = GetUploadPath(name);
GoogleCloudStorageMetadata metadata = new GoogleCloudStorageMetadata
{
name = uploadpath,
name = uploadPath,
acl = new GoogleCloudStorageAcl[]
{
new GoogleCloudStorageAcl
@ -130,20 +123,13 @@ public override UploadResult Upload(Stream stream, string fileName)
}
};
string metadatajson = JsonConvert.SerializeObject(metadata);
string serializedGoogleCloudStorageMetadata = JsonConvert.SerializeObject(googleCloudStorageMetadata);
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart", stream, fileName, "file",
headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: metadatajson);
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart&fields=name", stream, fileName, null, headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: serializedGoogleCloudStorageMetadata);
GoogleCloudStorageResponse upload = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response);
GoogleCloudStorageResponse googleCloudStorageResponse = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response);
if (upload.name != uploadpath)
{
Errors.Add("Upload failed.");
return null;
}
result.URL = GenerateURL(uploadpath);
result.URL = GenerateURL(googleCloudStorageResponse.name);
return result;
}
@ -151,6 +137,14 @@ public override UploadResult Upload(Stream stream, string fileName)
private string GetUploadPath(string fileName)
{
string uploadPath = NameParser.Parse(NameParserType.FolderPath, Prefix.Trim('/'));
if ((RemoveExtensionImage && Helpers.IsImageFile(fileName)) ||
(RemoveExtensionText && Helpers.IsTextFile(fileName)) ||
(RemoveExtensionVideo && Helpers.IsVideoFile(fileName)))
{
fileName = Path.GetFileNameWithoutExtension(fileName);
}
return URLHelpers.CombineURL(uploadPath, fileName);
}

View file

@ -3137,16 +3137,19 @@ private void txtGoogleCloudStorageObjectPrefix_TextChanged(object sender, EventA
private void cbGoogleCloudStorageStripExtensionImage_CheckedChanged(object sender, EventArgs e)
{
Config.GoogleCloudStorageRemoveExtensionImage = cbGoogleCloudStorageStripExtensionImage.Checked;
UpdateGoogleCloudStorageStatus();
}
private void cbGoogleCloudStorageStripExtensionVideo_CheckedChanged(object sender, EventArgs e)
{
Config.GoogleCloudStorageRemoveExtensionVideo = cbGoogleCloudStorageStripExtensionVideo.Checked;
UpdateGoogleCloudStorageStatus();
}
private void cbGoogleCloudStorageStripExtensionText_CheckedChanged(object sender, EventArgs e)
{
Config.GoogleCloudStorageRemoveExtensionText = cbGoogleCloudStorageStripExtensionText.Checked;
UpdateGoogleCloudStorageStatus();
}
#endregion Google Cloud Storage

View file

@ -265,7 +265,10 @@ private void UpdateGoogleCloudStorageStatus()
{
Bucket = Config.GoogleCloudStorageBucket,
Domain = Config.GoogleCloudStorageDomain,
Prefix = Config.GoogleCloudStorageObjectPrefix
Prefix = Config.GoogleCloudStorageObjectPrefix,
RemoveExtensionImage = Config.GoogleCloudStorageRemoveExtensionImage,
RemoveExtensionText = Config.GoogleCloudStorageRemoveExtensionText,
RemoveExtensionVideo = Config.GoogleCloudStorageRemoveExtensionVideo
};
lblGoogleCloudStoragePathPreview.Text = gcs.GetPreviewURL();

View file

@ -102,9 +102,13 @@ public GooglePhotosAlbum CreateAlbum(string albumName)
}
};
string serializedNewItemAlbum = JsonConvert.SerializeObject(newItemAlbum);
string serializedNewItemAlbumResponse = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/albums", serializedNewItemAlbum, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);
Dictionary<string, string> newItemAlbumArgs = new Dictionary<string, string>
{
{ "fields", "id" }
};
string serializedNewItemAlbum = JsonConvert.SerializeObject(newItemAlbum);
string serializedNewItemAlbumResponse = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/albums", serializedNewItemAlbum, UploadHelpers.ContentTypeJSON, newItemAlbumArgs, GoogleAuth.GetAuthHeaders());
GooglePhotosAlbum newItemAlbumResponse = JsonConvert.DeserializeObject<GooglePhotosAlbum>(serializedNewItemAlbumResponse);
return newItemAlbumResponse;
@ -116,17 +120,18 @@ public List<GooglePhotosAlbumInfo> GetAlbumList()
List<GooglePhotosAlbumInfo> albumList = new List<GooglePhotosAlbumInfo>();
Dictionary<string, string> args = new Dictionary<string, string>
Dictionary<string, string> albumListArgs = new Dictionary<string, string>
{
{ "excludeNonAppCreatedData", "true" }
{ "excludeNonAppCreatedData", "true" },
{ "fields", "albums(id,title,shareInfo),nextPageToken" }
};
string pageToken = "";
do
{
args["pageToken"] = pageToken;
string response = SendRequest(HttpMethod.GET, "https://photoslibrary.googleapis.com/v1/albums", args, headers: GoogleAuth.GetAuthHeaders());
albumListArgs["pageToken"] = pageToken;
string response = SendRequest(HttpMethod.GET, "https://photoslibrary.googleapis.com/v1/albums", albumListArgs, GoogleAuth.GetAuthHeaders());
pageToken = "";
if (!string.IsNullOrEmpty(response))
@ -167,10 +172,15 @@ public override UploadResult Upload(Stream stream, string fileName)
{
AlbumID = CreateAlbum(fileName).id;
Dictionary<string, string> albumOptionsResponseArgs = new Dictionary<string, string>
{
{ "fields", "shareInfo/shareableUrl" }
};
GooglePhotosAlbumOptions albumOptions = new GooglePhotosAlbumOptions();
string serializedAlbumOptions = JsonConvert.SerializeObject(albumOptions);
string serializedAlbumOptionsResponse = SendRequest(HttpMethod.POST, $"https://photoslibrary.googleapis.com/v1/albums/{AlbumID}:share", content: serializedAlbumOptions, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);
string serializedAlbumOptionsResponse = SendRequest(HttpMethod.POST, $"https://photoslibrary.googleapis.com/v1/albums/{AlbumID}:share", serializedAlbumOptions, UploadHelpers.ContentTypeJSON, albumOptionsResponseArgs, GoogleAuth.GetAuthHeaders());
GooglePhotosAlbumOptionsResponse albumOptionsResponse = JsonConvert.DeserializeObject<GooglePhotosAlbumOptionsResponse>(serializedAlbumOptionsResponse);
result.URL = albumOptionsResponse.shareInfo.shareableUrl;
@ -183,7 +193,7 @@ public override UploadResult Upload(Stream stream, string fileName)
{ "Authorization", GoogleAuth.GetAuthHeaders()["Authorization"] }
};
string uploadToken = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/uploads", stream, contentType: UploadHelpers.ContentTypeOctetStream, headers: uploadTokenHeaders);
string uploadToken = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/uploads", stream, UploadHelpers.ContentTypeOctetStream, null, uploadTokenHeaders);
GooglePhotosNewMediaItemRequest newMediaItemRequest = new GooglePhotosNewMediaItemRequest
{
@ -200,9 +210,14 @@ public override UploadResult Upload(Stream stream, string fileName)
}
};
Dictionary<string, string> newMediaItemRequestArgs = new Dictionary<string, string>
{
{ "fields", "newMediaItemResults(mediaItem/productUrl)" }
};
string serializedNewMediaItemRequest = JsonConvert.SerializeObject(newMediaItemRequest);
result.Response = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", serializedNewMediaItemRequest, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);
result.Response = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", serializedNewMediaItemRequest, UploadHelpers.ContentTypeJSON, newMediaItemRequestArgs, GoogleAuth.GetAuthHeaders());
GooglePhotosNewMediaItemResults newMediaItemResult = JsonConvert.DeserializeObject<GooglePhotosNewMediaItemResults>(result.Response);

View file

@ -91,7 +91,7 @@ public override UploadResult ShortenURL(string url)
{
UploadResult result = new UploadResult { URL = url };
FirebaseRequest request = new FirebaseRequest
FirebaseRequest requestOptions = new FirebaseRequest
{
dynamicLinkInfo = new DynamicLinkInfo
{
@ -102,7 +102,7 @@ public override UploadResult ShortenURL(string url)
if (IsShort)
{
request.suffix = new FirebaseSuffix
requestOptions.suffix = new FirebaseSuffix
{
option = "SHORT"
};
@ -110,11 +110,13 @@ public override UploadResult ShortenURL(string url)
Dictionary<string, string> args = new Dictionary<string, string>
{
{ "key", WebAPIKey }
{ "key", WebAPIKey },
{ "fields", "shortLink" }
};
string requestjson = JsonConvert.SerializeObject(request);
result.Response = SendRequest(HttpMethod.POST, "https://firebasedynamiclinks.googleapis.com/v1/shortLinks", requestjson, UploadHelpers.ContentTypeJSON, args);
string serializedRequestOptions = JsonConvert.SerializeObject(requestOptions);
result.Response = SendRequest(HttpMethod.POST, "https://firebasedynamiclinks.googleapis.com/v1/shortLinks", serializedRequestOptions, UploadHelpers.ContentTypeJSON, args);
FirebaseResponse firebaseResponse = JsonConvert.DeserializeObject<FirebaseResponse>(result.Response);
if (firebaseResponse != null)

View file

@ -455,7 +455,7 @@ public class TaskSettingsAdvanced
Editor("System.Windows.Forms.Design.StringCollectionEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public List<string> ImageExtensions { get; set; }
[Category("Upload"), DefaultValue(false), Description("Copy URL before start upload. Only works for FTP, FTPS, SFTP and Dropbox public URLs.")]
[Category("Upload"), DefaultValue(false), Description("Copy URL before start upload. Only works for FTP, FTPS, SFTP, Dropbox, and Google Cloud Storage.")]
public bool EarlyCopyURL { get; set; }
[Category("Upload"), Description("Files with these file extensions will be uploaded using text uploader."),