Exemple #1
0
        public async Task <string> CompressAsync(long DestinationFolderID, string Filename = null)
        {
            var GeneJobID  = Utilitiez.RandomString(8);
            var parameters = new AuthDictionary
            {
                { "folderids", string.Join(",", FolderIDs) },
                { "tofolderid", DestinationFolderID.ToString() },
                { "toname", Filename },
                { "progresshash", GeneJobID }
            };

            using (HtpClient localHttpClient = new HtpClient(new HCHandler()))
            {
                HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Post, new pUri("/savezip"));
                HtpReqMessage.Content = new FormUrlEncodedContent(parameters);
                using (HttpResponseMessage response = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false))
                {
                    string result = await response.Content.ReadAsStringAsync();

                    if (response.StatusCode == System.Net.HttpStatusCode.OK && JObject.Parse(result).Value <int>("result").Equals(0))
                    {
                        return(GeneJobID);
                    }
                    else
                    {
                        ShowError(result);
                        return(null);
                    }
                }
            }
        }
        public async Task DownloadAsZip(Uri publiclink, string FileSaveDir, IProgress <ReportStatus> ReportCls = null, CancellationToken token = default)
        {
            string FileName = $"pCloudPublicLink_{Utilitiez.RandomString(3)}.zip";

            ReportCls = ReportCls ?? new Progress <ReportStatus>();
            ReportCls.Report(new ReportStatus {
                Finished = false, TextStatus = "Initializing..."
            });
            try
            {
                var progressHandler = new ProgressMessageHandler(new HCHandler());
                progressHandler.HttpReceiveProgress += (sender, e) => { ReportCls.Report(new ReportStatus {
                        ProgressPercentage = e.ProgressPercentage, BytesTransferred = e.BytesTransferred, TotalBytes = e.TotalBytes ?? 0, TextStatus = "Downloading..."
                    }); };
                var localHttpClient = new HtpClient(progressHandler);
                HttpRequestMessage HtpReqMessage = new HttpRequestMessage(HttpMethod.Get, new pUri("/getpubzip", new AuthDictionary()
                {
                    { "code", publiclink.GetParameterInUrl("code") }, { "filename", FileName }
                }));

                using (HttpResponseMessage ResPonse = await localHttpClient.SendAsync(HtpReqMessage, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false))
                {
                    if (ResPonse.IsSuccessStatusCode)
                    {
                        ReportCls.Report(new ReportStatus {
                            Finished = true, TextStatus = $"[{FileName}] Downloaded successfully."
                        });
                    }
                    else
                    {
                        ReportCls.Report(new ReportStatus {
                            Finished = true, TextStatus = $"Error code: {ResPonse.StatusCode}"
                        });
                    }
                    ResPonse.EnsureSuccessStatusCode();
                    Stream stream_ = await ResPonse.Content.ReadAsStreamAsync();

                    var FPathname = Path.Combine(FileSaveDir, FileName);
                    using (FileStream fileStream = new FileStream(FPathname, FileMode.Append, FileAccess.Write))
                    {
                        stream_.CopyTo(fileStream);
                    }
                }
            }
            catch (Exception ex)
            {
                ReportCls.Report(new ReportStatus {
                    Finished = true
                });
                if (!ex.Message.ToString().ToLower().Contains("a task was canceled"))
                {
                    throw new pCloudException(ex.Message, 1001);
                }
                ReportCls.Report(new ReportStatus {
                    TextStatus = ex.Message
                });
            }
        }
Exemple #3
0
 public pUri(string ApiAction, Dictionary <string, string> Parameters) : base(APIbase + ApiAction + Utilitiez.AsQueryString(Parameters))
 {
 }