public async Task <IHistory> UploadFileAsync(IUploadPreset preset, string path, CancellationToken cancellationToken, IProgress <double> progressCallback = null) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException(nameof(path)); } var fileName = Path.GetFileName(path); var link = await GetAsync <Link>(preset as YandexPreset, "https://cloud-api.yandex.net/v1/disk/resources/upload?path=app:/" + fileName + "&overwrite=true", cancellationToken); if (string.IsNullOrEmpty(link?.Href)) { throw new UploadException("Unknown error"); } await using (var fileSteram = new FileStream(path, FileMode.Open, FileAccess.Read)) { await PutAsync(preset as YandexPreset, link.Href, new StreamContent(fileSteram), cancellationToken); } var downloadLink = await GetAsync <Link>(preset as YandexPreset, "https://cloud-api.yandex.net/v1/disk/resources/download?path=app:/" + fileName, cancellationToken); var history = new History { Type = preset.Type, PresetName = preset.Title, DateInUtc = DateTime.UtcNow, Result = 200, Link = downloadLink.Href }; return(history); }
public static async Task <bool> IsAuthorized(IUploadPreset preset) { if (preset is not GfycatPreset gfycatPreset) { return(false); } //When in anonymous mode, only the access token is used. if (preset.IsAnonymous) { //If the access token is still valid, no need to refresh it. if (!string.IsNullOrWhiteSpace(gfycatPreset.AccessToken) && IsAuthorizationExpired(gfycatPreset)) { return(true); } if (!await GetTokens(gfycatPreset)) { return(false); } } //When in authenticated mode, if there's no refresh token, it means that the app if not authorized. if (string.IsNullOrWhiteSpace(gfycatPreset.RefreshToken)) { return(false); } if (!IsAuthorizationExpired(gfycatPreset)) { return(true); } return(await RefreshToken(gfycatPreset)); }
public async Task <IHistory> UploadFileAsync(IUploadPreset preset, string path, CancellationToken cancellationToken, IProgress <double> progressCallback = null) { if (preset is not ImgurPreset imgurPreset) { throw new Exception("Imgur preset is null."); } var args = new Dictionary <string, string>(); var headers = new NameValueCollection(); if (!preset.IsAnonymous) { if (!await IsAuthorized(imgurPreset)) { throw new UploadException("It was not possible to get the authorization to upload to Imgur."); } headers.Add("Authorization", "Bearer " + imgurPreset.AccessToken); if (imgurPreset.UploadToAlbum) { var album = string.IsNullOrWhiteSpace(imgurPreset.SelectedAlbum) || imgurPreset.SelectedAlbum == "♥♦♣♠" ? await AskForAlbum(imgurPreset) : imgurPreset.SelectedAlbum; if (!string.IsNullOrEmpty(album)) { args.Add("album", album); } } } else { headers.Add("Authorization", "Client-ID " + Secret.ImgurId); } if (cancellationToken.IsCancellationRequested) { return(null); } return(await Upload(imgurPreset, path, args, headers)); }
public async Task <IHistory> UploadFileAsync(IUploadPreset preset, string path, CancellationToken cancellationToken, IProgress <double> progressCallback = null) { if (preset is not GfycatPreset gfycatPreset) { throw new Exception("Gfycat preset is null."); } if (!await IsAuthorized(gfycatPreset)) { throw new UploadException("It was not possible to get the authorization to upload to Gfycat."); } var headers = new NameValueCollection { { "Authorization", "Bearer " + gfycatPreset.AccessToken } }; if (cancellationToken.IsCancellationRequested) { return(null); } return(await Upload(gfycatPreset, path, headers)); }