Example #1
0
        public static async Task <SavesClass> UploadFilesAsync(string PathFileToUpload, string Username, ProgressBar PB, Label LBL)
        {
            string Filename = Path.GetFileName(PathFileToUpload);
            string DLink, FileSize;

            try
            {
                using (MALogin MAL = new MALogin())
                {
                    MegaApiClient client = MAL.Login();
                    INode         Folder = await Task.Run(() => ChkIfFolderAndFileExist(client, Username, Filename));

                    if (Folder != null)
                    {
                        Progress <double> ze = new Progress <double>(p => PB.Dispatcher.Invoke(DispatcherPriority.Normal,
                                                                                               new Action(() => { UpdateMessages.UploadMsg((int)p, PB, LBL); })));
                        using (FileStream stream = File.Open(@PathFileToUpload, FileMode.Open))
                        {
                            INode FileUploaded = await client.UploadAsync(stream, Filename, Folder, ze); //Uploading File from PC to Mega

                            //INode FileUploaded = await client.UploadFileAsync(PathFileToUpload, Folder, ze); //Uploading File from PC to Mega
                            if (FileUploaded != null)
                            {
                                DLink    = (await client.GetDownloadLinkAsync(FileUploaded)).ToString();
                                FileSize = (((int)(FileUploaded.Size / 1024))).ToString(); //Size in Kb
                                client.Logout();
                                GC.Collect();                                              // TENGO QUE LLAMAR ESTO O NO DEJA BORRAR EL ARCHIVO ZIP PORQUE DICE QUE AUN ESTA EN USO.
                                if (!DLink.Equals(""))
                                {
                                    SavesClass SC = new SavesClass();
                                    SC.DLink = DLink;
                                    SC.Size  = FileSize;
                                    return(SC);
                                }
                                else
                                {
                                    PB.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { UpdateMessages.UploadMsg(101, PB, LBL); }));
                                    return(null);
                                }
                            }
                            PB.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { UpdateMessages.UploadMsg(101, PB, LBL); }));
                            return(null);
                        }
                    }
                    else
                    {
                        PB.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { UpdateMessages.UploadMsg(101, PB, LBL); }));
                        return(null);
                    }
                }
            }
            catch
            {
                PB.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { UpdateMessages.UploadMsg(102, PB, LBL); }));
                return(null);
            }
        }
Example #2
0
        public static async Task <bool> DeleteSelSave(SavesClass SV, ProgressBar PB, Label LBL)
        {
            UpdateMessages.DeleteMsg(10, PB, LBL);
            SavesContainer SVC = new SavesContainer();

            try
            {
                SV.IsDelete = "1";
                string SV_obj = "";

                SV_obj = SV_obj + JsonConvert.SerializeObject(SV, Formatting.Indented);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data/" + WebUtility.UrlEncode(SV.FileName)));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Headers.Add("authorization", Constants.ApiKey);
                request.Method = "POST";

                byte[] byteArray = Encoding.UTF8.GetBytes(SV_obj);
                request.ContentLength = byteArray.Length;
                using (Stream DataStream = await request.GetRequestStreamAsync())
                {
                    DataStream.Write(byteArray, 0, byteArray.Length);
                    DataStream.Close();
                    UpdateMessages.DeleteMsg(50, PB, LBL);
                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream ResponseStream = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(ResponseStream))
                            {
                                string ServerResponse = await sr.ReadToEndAsync();

                                SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                                if (SVC.status.Equals("1"))
                                {
                                    //Success
                                    using (MALogin MAL = new MALogin())
                                    {
                                        MegaApiClient client = MAL.Login();
                                        //DELETE FILE FROM MEGA
                                        var nodes    = client.GetNodes();
                                        var SelNodes = nodes.First(n => n.Name == SV.FileName && n.Type == NodeType.File);
                                        await client.DeleteAsync(SelNodes, false);

                                        client.Logout();
                                    }
                                    UpdateMessages.DeleteMsg(100, PB, LBL);
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.DeleteMsg(103, PB, LBL);
                                    return(true);
                                }
                                else
                                {
                                    //Fail
                                    UpdateMessages.DeleteMsg(101, PB, LBL);
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.DeleteMsg(103, PB, LBL);
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException e)
            {
                UpdateMessages.DeleteMsg(102, PB, LBL);
                //MessageBox.Show("Save could not be deleted due to the following error:" + e.Message);
                if (e.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)e.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string error = reader.ReadToEnd();
                            SVC = JsonConvert.DeserializeObject <SavesContainer>(error);


                            MessageBox.Show(SVC.message);

                            return(false);
                        }
                    }
                }
                UpdateMessages.DeleteMsg(103, PB, LBL);
                return(false);
            }
        }