public async Task <string> ThumbnailUrl(string WIDTH_x_HEIGHT, ExtEnum Ext, bool Crop) { var parameters = new AuthDictionary() { { "fileid", FileID.ToString() }, { "size", WIDTH_x_HEIGHT }, { "type", Ext.ToString() }, { "crop", Crop ? "1" : null } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/getthumblink")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(string.Format("https://{0}{1}", JObject.Parse(result).SelectToken("hosts[0]").ToString(), JObject.Parse(result).SelectToken("path").ToString().Replace(@"\", string.Empty))); } else { ShowError(result); return(null); } } } }
public async Task <JSON_FileMetadata> Copy(long DestinationFolderID, bool AutoRename, string RenameTo = null) { var parameters = new AuthDictionary { { "fileid", FileID.ToString() }, { "tofolderid", DestinationFolderID.ToString() }, { "noover", AutoRename ? "1" : null }, { "toname", RenameTo } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/copyfile")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(JsonConvert.DeserializeObject <JSON_FileMetadata>(JObject.Parse(result).SelectToken("metadata").ToString(), JSONhandler)); } else { ShowError(result); return(null); } } } }
public async Task <string> UnCompressAsync(long ExtractIntoFolderID, string ArchivePassword = null, IfExistsEnum IfExists = IfExistsEnum.rename) { var GeneJobID = RandomString(8); var parameters = new AuthDictionary { { "password", ArchivePassword ?? null }, { "tofolderid", ExtractIntoFolderID.ToString() }, { "nooutput", "1" }, { "overwrite", IfExists.ToString() }, { "progresshash", GeneJobID } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/extractarchive")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(GeneJobID); } else { ShowError(result); return(null); } } } }
public async Task <string> Public(int?MaxDownloads = null, long?MaxTrafficInBytes = null) { var parameters = new AuthDictionary { { "fileid", FileID.ToString() }, { "maxdownloads", MaxDownloads.HasValue?MaxDownloads.Value.ToString():null }, { "maxtraffic", MaxTrafficInBytes.HasValue ? MaxTrafficInBytes.Value.ToString() : null } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/getfilepublink")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(JObject.Parse(result).SelectToken("link").ToString().Replace(@"\", string.Empty)); } else { ShowError(result); return(null); } } } }
public async Task <string> FileInFolderDirectUrl(Uri folderpubliclink, long fileid) { var parameters = new AuthDictionary { { "code", folderpubliclink.GetParameterInUrl("code") }, { "fileid", fileid.ToString() }, { "forcedownload", "1" } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/getfilepublink")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(string.Format("https://{0}{1}", JObject.Parse(result).SelectToken("hosts[0]").ToString(), JObject.Parse(result).SelectToken("path").ToString().Replace(@"\", string.Empty))); } else { ShowError(result); return(null); } } } }
public async Task <JSON_FileMetadata> CopyZip(Uri publiclink, long DestinationFolderID, string RenameTo = null) { var parameters = new AuthDictionary { { "code", publiclink.GetParameterInUrl("code") }, { "tofolderid", DestinationFolderID.ToString() } }; if (!string.IsNullOrEmpty(RenameTo)) { parameters.Add("toname", RenameTo.ToLower().EndsWith(".zip") ? RenameTo : $"{RenameTo}.zip"); } using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/savepubzip")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(JsonConvert.DeserializeObject <JSON_FileMetadata>(JObject.Parse(result).SelectToken("metadata").ToString(), JSONhandler)); } else { ShowError(result); return(null); } } } }
public async Task <JSON_PublicMetadata> Metadata(Uri publiclink) { var parameters = new AuthDictionary { { "code", publiclink.GetParameterInUrl("code") } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/getfilepublink")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { return(JsonConvert.DeserializeObject <JSON_PublicMetadata>(JObject.Parse(result).SelectToken("metadata").ToString(), JSONhandler)); } else { ShowError(result); return(null); } } } }
public async Task <byte[]> Thumbnail(string WIDTH_x_HEIGHT, ExtEnum Ext, bool Crop) { var parameters = new AuthDictionary() { { "fileid", FileID.ToString() }, { "size", WIDTH_x_HEIGHT }, { "type", Ext.ToString() }, { "crop", Crop ? "1" : null } }; using (HtpClient localHttpClient = new HtpClient(new HCHandler())) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/getthumb")); HtpReqMessage.Content = new FormUrlEncodedContent(parameters.RemoveEmptyValues()); using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)) { if (response.StatusCode == HttpStatusCode.OK) { return(await response.Content.ReadAsByteArrayAsync()); } else { throw new pCloudException(response.ReasonPhrase, (int)response.StatusCode); } } } }
public async Task <List <JSON_FileMetadata> > UploadLocal(object FileToUP, SentType TheUpType, string Filename, bool AutoRename = true, IProgress <ReportStatus> ReportCls = null, CancellationToken token = default) { var parameters = new AuthDictionary { { "folderid", FolderID.ToString() }, { "filename", Filename }, { "nopartial", "1" }, { "renameifexists", AutoRename ? "1" : null } }; ReportCls = ReportCls ?? new Progress <ReportStatus>(); ReportCls.Report(new ReportStatus() { Finished = false, TextStatus = "Initializing..." }); try { System.Net.Http.Handlers.ProgressMessageHandler progressHandler = new System.Net.Http.Handlers.ProgressMessageHandler(new HCHandler()); progressHandler.HttpSendProgress += (sender, e) => { ReportCls.Report(new ReportStatus() { ProgressPercentage = e.ProgressPercentage, BytesTransferred = e.BytesTransferred, TotalBytes = e.TotalBytes ?? 0, TextStatus = "Uploading..." }); }; using (HttpClient localHttpClient = new HtpClient(progressHandler)) { HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/uploadfile", parameters.RemoveEmptyValues())); // ''''''''''''''''''''''''''''''''' HttpContent streamContent = null; switch (TheUpType) { case SentType.filepath: streamContent = new StreamContent(new System.IO.FileStream(FileToUP.ToString(), System.IO.FileMode.Open, System.IO.FileAccess.Read)); break; case SentType.memorystream: streamContent = new StreamContent((System.IO.Stream)FileToUP); break; case SentType.bytesArray: streamContent = new StreamContent(new System.IO.MemoryStream((byte[])FileToUP)); break; } streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); HtpReqMessage.Content = streamContent; // ''''''''''''''''will write the whole content to H.D WHEN download completed''''''''''''''''''''''''''''' using (HttpResponseMessage ResPonse = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false)) { string result = await ResPonse.Content.ReadAsStringAsync(); token.ThrowIfCancellationRequested(); if (ResPonse.StatusCode == HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0)) { ReportCls.Report(new ReportStatus() { Finished = true, TextStatus = "Upload completed successfully" }); return(JsonConvert.DeserializeObject <List <JSON_FileMetadata> >(JObject.Parse(result).SelectToken("metadata").ToString(), JSONhandler)); } else { ReportCls.Report(new ReportStatus() { Finished = true, TextStatus = $"The request returned with HTTP status code {ResPonse.ReasonPhrase}" }); ShowError(result); return(null); } } } } catch (Exception ex) { ReportCls.Report(new ReportStatus() { Finished = true }); if (ex.Message.ToString().ToLower().Contains("a task was canceled")) { ReportCls.Report(new ReportStatus() { TextStatus = ex.Message }); } else { throw new pCloudException(ex.Message, 1001); } return(null); } }