예제 #1
0
        /// <summary>
        /// Scarica un file dal Cloud.
        /// </summary>
        /// <param name="FileName">Nome del file da scaricare</param>
        /// <param name="DestFileName">Nome del file di destinazione</param>
        /// <returns>Restituisce FileDownloaded se l'operazione ha successo, altrimenti restituisce
        /// FileDownloadError in caso di errore o FileNotFound se il file non esiste</returns>
        public async Task <CloudFileStatus> GetFile(string FileName, string DestFileName)
        {
            const string UrlRoot = URLBase + "/me/drive/special/approot:/";

            if (!await CheckToken())
            {
                Debug.WriteLine("User is not logged in!");
                return(CloudFileStatus.FileDownloadError);
            }

            try
            {
                CloudFileStatus Result = await FileExists(FileName);

                if (Result == CloudFileStatus.FileError || Result == CloudFileStatus.FileNotFound)
                {
                    throw new Exception(string.Format(string.Format("{0}: Unable to download the file {1}. Error: {2}", AppResources.errCloudDownload, FileName, GetLastError())));
                }

                // Posso scaricare il file
                if (!await DownloadFile(UrlRoot + FileName, DestFileName))
                {
                    Result = CloudFileStatus.FileDownloadError;
                }
                return(CloudFileStatus.FileDownloaded);
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format("GetFile error: {0}", e.Message));
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// Scarica un file dal Cloud.
        /// </summary>
        /// <param name="FileName">Nome del file da scaricare</param>
        /// <param name="DestFileName">Nome del file di destinazione</param>
        /// <returns>Restituisce FileDownloaded se l'operazione ha successo, altrimenti restituisce
        /// FileDownloadError in caso di errore o FileNotFound se il file non esiste</returns>
        public async Task <CloudFileStatus> GetFile(string FileName, string DestFileName)
        {
            if (!await CheckToken())
            {
                Debug.WriteLine("User is not logged in!");
                return(CloudFileStatus.FileDownloadError);
            }

            try
            {
                DriveFileInfo driveFileInfo = await GetFileInfo(FileName);

                CloudFileStatus Result = driveFileInfo.Status;
                if (Result == CloudFileStatus.FileError || Result == CloudFileStatus.FileNotFound)
                {
                    throw new Exception(string.Format(string.Format("{0}: Unable to download the file {1}. Error: {2}", AppResources.errCloudDownload, FileName, GetLastError())));
                }

                // Posso scaricare il file
                if (!await DownloadFile(driveFileInfo.FileID, DestFileName))
                {
                    Result = CloudFileStatus.FileDownloadError;
                }
                return(CloudFileStatus.FileDownloaded);
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format("GetFile error: {0}", e.Message));
                throw;
            }
        }
예제 #3
0
        /// <summary>
        /// Verifica se un determinato file esiste su OneDrive.
        /// </summary>
        /// <param name="FileName">Nome del file</param>
        /// <returns>Restituisce FileNotFound se il file non viene trovato, FileFound se esiste
        /// o FileError in caso di errore. Chiamare la funzione GetLastError per informazioni sull'eventuale
        /// errore.</returns>
        public async Task <CloudFileStatus> FileExists(string FileName)
        {
            LastError = "";
            const string UrlRoot = URLBase + "/me/drive/special/approot/";

            CloudFileStatus Result = CloudFileStatus.FileNotFound;

            if (!await CheckToken())
            {
                LastError = "User is not logged in!";
                Debug.WriteLine(LastError);
                return(CloudFileStatus.FileError);
            }

            // Ottiene la lista dei file nella cartella dedicata all'App
            HttpClient appClient = new HttpClient();

            try
            {
                string Url = UrlRoot + "children";
                if (string.Compare(Path.GetFileName(FileName), FileName) != 0)
                {
                    string[] tokens = FileName.Split(new [] { Path.DirectorySeparatorChar });
                    if (tokens.Length > 1)
                    {
                        Url = URLBase + "/me/drive/special/approot:";
                        for (int i = 0; i < tokens.Length - 1; i++)
                        {
                            if (tokens[i].Length > 0)
                            {
                                Url += "/" + tokens[i];
                            }
                        }
                        Url     += ":/children/";
                        FileName = tokens[tokens.Length - 1];
                    }
                }

                appClient.DefaultRequestHeaders.Clear();
                appClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Account.AccessToken);

                HttpResponseMessage Response = await appClient.GetAsync(new Uri(Url));

                if (!Response.IsSuccessStatusCode)
                {
                    appClient.Dispose();
                    LastError = "Unable to find the file " + FileName + ". Status code: " + Response.StatusCode;
                    Debug.WriteLine(LastError);
                    return(CloudFileStatus.FileError);
                }

                // Cerca nel JSON il file
                JObject JsonObj = JObject.Parse(await Response.Content.ReadAsStringAsync());
                if (JsonObj != null)
                {
                    JArray DriveItems = JArray.Parse(JsonObj["value"].ToString());
                    if (DriveItems != null)
                    {
                        Result = CloudFileStatus.FileNotFound;

                        foreach (JObject JsonDriveItem in DriveItems)
                        {
                            if (JsonDriveItem != null)
                            {
                                if (JsonDriveItem["name"].ToString().CompareTo(FileName) == 0)
                                {
                                    // Trovato!
                                    Result = CloudFileStatus.FileFound;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    appClient.Dispose();
                    LastError = "Unable to find the file " + FileName + ". Malformed response";
                    Debug.WriteLine(LastError);
                    return(CloudFileStatus.FileError);
                }
            }
            catch (Exception Ex)
            {
                appClient.Dispose();
                LastError = "Unable to find the file " + FileName + ": " + Ex.Message;
                Debug.WriteLine(LastError);
                return(CloudFileStatus.FileError);
            }

            appClient.Dispose();
            return(Result);
        }