Exemplo n.º 1
0
        public string GetFolderPath(OneDriveFolder folder, List <OneDriveFolder> folders)
        {
            string         path         = "";
            string         top          = folders[0].id;
            OneDriveFolder searchfolder = folder;

            while (searchfolder != null)
            {
                path         = searchfolder.name + "/" + path;
                searchfolder = GetFolder(searchfolder.parent, folders);
            }
            return("$/" + path);
        }
Exemplo n.º 2
0
        private async void btnCreateMap_Click(object sender, EventArgs e)
        {
            if (this.liveConnectClient == null)
            {
                SigninButton_Click(sender, e);
                return;
            }
            signinButton.Enabled  = false;
            signOutButton.Enabled = false;
            btnCreateMap.Enabled  = false;
            txtRoot.Enabled       = false;
            StringBuilder sb     = new StringBuilder();
            string        status = "";

            sb.AppendLine(String.Format("Started: {0}", DateTime.Now.ToShortTimeString()));

            richTextStatus.Text = sb.ToString();
            richTextStatus.Refresh();

            if (txtRoot.Text.Trim().Length == 0)
            {
                MessageBox.Show("The root path is the relative path of the top folder to map\n Example: Karaoke/CDG_ZIP/Christmas", "Enter root path in the text box");
                signinButton.Enabled  = true;
                signOutButton.Enabled = true;
                btnCreateMap.Enabled  = true;
                txtRoot.Enabled       = true;
                return;
            }
            string rootpath = txtRoot.Text.Trim();

            string[] rootpaths = rootpath.Split('/');
            rootpath = "me/skydrive";
            OneDriveFolder rootfolder = null;

            foreach (string root in rootpaths)
            {
                sb.AppendLine(String.Format("Finding root: {0}", root));
                richTextStatus.Text = sb.ToString();
                richTextStatus.Refresh();
                LiveOperationResult   rootresult  = null;
                List <OneDriveFolder> rootfolders = new List <OneDriveFolder>();
                try
                {
                    rootresult = await this.liveConnectClient.GetAsync(string.Format("{0}/files", rootpath));

                    if (rootresult != null && rootresult.Result != null && rootresult.Result["data"] != null)
                    {
                        foreach (KeyValuePair <string, object> o in rootresult.Result)
                        {
                            if (o.Key == "data")
                            {
                                if (o.Value != null && o.Value is List <object> )
                                {
                                    foreach (object obj in o.Value as List <object> )
                                    {
                                        string id     = "";
                                        string name   = "";
                                        string type   = "";
                                        string link   = "";
                                        string source = "";
                                        foreach (KeyValuePair <string, object> kvp in obj as IDictionary <string, object> )
                                        {
                                            switch (kvp.Key)
                                            {
                                            case "id":
                                                id = kvp.Value.ToString();
                                                break;

                                            case "name":
                                                name = kvp.Value.ToString();
                                                break;

                                            case "type":
                                                type = kvp.Value.ToString();
                                                break;

                                            case "link":
                                                link = kvp.Value.ToString();
                                                break;

                                            case "source":
                                                source = kvp.Value.ToString();
                                                break;
                                            }
                                        }
                                        if (type == "folder")
                                        {
                                            rootfolders.Add(new OneDriveFolder("root", id, name, link));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                bool found = false;
                foreach (OneDriveFolder f in rootfolders)
                {
                    if (f.name == root)
                    {
                        rootfolder = f;
                        rootpath   = f.id;
                        found      = true;
                        break;
                    }
                }
                if (found == false)
                {
                    rootfolder = null;
                }
            }

            if (rootfolder == null)
            {
                MessageBox.Show("Root Folder Not Found: " + txtRoot.Text, "Root not found");
                return;
            }

            //folder.a645bfc91d3b795a.A645BFC91D3B795A!41650/files //Sample path (using id)
            //https://onedrive.live.com/redir?resid=A645BFC91D3B795A!41650&authkey=!ABx2Nb-uPkoa7RA&ithint=folder%2ctxt //Sample link
            List <OneDriveFolder> folders = new List <OneDriveFolder>();

            folders.Add(rootfolder);
            int ix = 0;

            try
            {
                while (ix < folders.Count)
                {
                    LiveOperationResult result = await this.liveConnectClient.GetAsync(string.Format("{0}/files", folders[ix].id));

                    if (result != null && result.Result != null && result.Result["data"] != null)
                    {
                        foreach (KeyValuePair <string, object> o in result.Result)
                        {
                            if (o.Key == "data")
                            {
                                if (o.Value != null && o.Value is List <object> )
                                {
                                    foreach (object obj in o.Value as List <object> )
                                    {
                                        string id     = "";
                                        string name   = "";
                                        string type   = "";
                                        string link   = "";
                                        string source = "";
                                        foreach (KeyValuePair <string, object> kvp in obj as IDictionary <string, object> )
                                        {
                                            switch (kvp.Key)
                                            {
                                            case "id":
                                                id = kvp.Value.ToString();
                                                break;

                                            case "name":
                                                name = kvp.Value.ToString();
                                                break;

                                            case "type":
                                                type = kvp.Value.ToString();
                                                break;

                                            case "link":
                                                link = kvp.Value.ToString();
                                                break;

                                            case "source":
                                                source = kvp.Value.ToString();
                                                break;
                                            }
                                        }
                                        if (type == "folder")
                                        {
                                            folders.Add(new OneDriveFolder(folders[ix].id, id, name, link));
                                            status = String.Format("\nFolder: {0}\n", name);
                                            richTextStatus.Text = sb.ToString() + status;
                                            richTextStatus.Refresh();
                                        }
                                        else if (type == "file" || type == "video")
                                        {
                                            status = String.Format("\nFile: {0}\n", name);
                                            richTextStatus.Text = sb.ToString() + status;
                                            richTextStatus.Refresh();
                                            OneDriveFile odf = new OneDriveFile(folders[ix].id, id, name, link, source);
                                            if (odf.name.ToLower().Contains(".mp4"))
                                            {
                                                folders[ix].files.Add(odf);
                                            }

                                            /*
                                             * LiveOperationResult resultlink = null;
                                             * for (int i = 0; i < 3; i++)
                                             * {
                                             *  try
                                             *  {
                                             *      resultlink = await this.liveConnectClient.GetAsync(string.Format("{0}/shared_read_link", folders[ix].id));
                                             *      if (resultlink != null && resultlink.RawResult != null && resultlink.RawResult.Length > 0)
                                             *          break;
                                             *      else
                                             *          Thread.Sleep(5000);
                                             *  }
                                             *  catch (Exception ex)
                                             *  {
                                             *      MessageBox.Show(ex.Message, "Navigation failed - will try to continue");
                                             *      Thread.Sleep(5000);
                                             *  }
                                             * }
                                             * if (resultlink != null && resultlink.RawResult != null && resultlink.RawResult.Length > 0)
                                             * {
                                             *  string ilink = "link\": ";
                                             *  string raw = resultlink.RawResult.Trim();
                                             *  int iof = raw.IndexOf(ilink);
                                             *  if (iof > 0)
                                             *  {
                                             *      iof += ilink.Length + 1;
                                             *      int end = raw.LastIndexOf('"');
                                             *      if (end > 0 && end > iof)
                                             *      {
                                             *          odf.link = raw.Substring(iof, end - iof);
                                             *          folders[ix].files.Add(odf);
                                             *      }
                                             *      else
                                             *      {
                                             *          MessageBox.Show("Operation will continue", "Unexpected start/end result");
                                             *      }
                                             *  }
                                             *  else
                                             *  {
                                             *      MessageBox.Show("Operation will continue", "Unexpected parsing result");
                                             *  }
                                             * }
                                             */
                                        }
                                        else
                                        {
                                            MessageBox.Show("Unknow file type: " + type, "Unexpected file type");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    ix++;
                }
                if (folders != null && folders.Count > 0 && folders[0].name != null && folders[0].name.Trim().Length > 0)
                {
                    sb.AppendLine(String.Format("Writing: {0}", string.Format(@"C:\Temp\{0}.txt", folders[0].name)));
                    richTextStatus.Text = sb.ToString();
                    richTextStatus.Refresh();
                    StreamWriter sw     = File.CreateText(string.Format(@"C:\Temp\{0}.txt", folders[0].name));
                    int          nfiles = 0;
                    foreach (OneDriveFolder folder in folders)
                    {
                        string path = GetFolderPath(folder, folders);
                        foreach (OneDriveFile file in folder.files)
                        {
                            if (file.name.Trim().EndsWith(".mp4", StringComparison.CurrentCultureIgnoreCase) == true)
                            {
                                sw.WriteLine(string.Format("{0}\t{1}", GetPath(path, file), file.link));
                                nfiles++;
                            }
                        }
                    }

                    /*
                     * sw.WriteLine();
                     * sw.WriteLine("----");
                     * sw.WriteLine();
                     * int nfiles = 0;
                     * foreach (OneDriveFolder folder in folders)
                     * {
                     *  string path = GetFolderPath(folder, folders);
                     *  sw.WriteLine(string.Format("{0}", path));
                     *  foreach (OneDriveFile file in folder.files)
                     *  {
                     *      if (file.name.Trim().EndsWith(".mp4", StringComparison.CurrentCultureIgnoreCase) == true)
                     *      {
                     *          sw.WriteLine(string.Format("\t{0}\t{1}", GetPath(path, file), file.link));
                     *          nfiles++;
                     *      }
                     *  }
                     * }
                     */
                    sw.Close();
                    sb.AppendLine();
                    sb.AppendLine(String.Format("Folders: {0}", string.Format(@"C:\Temp\{0}.txt", folders.Count)));
                    sb.AppendLine(String.Format("Files  : {0}", string.Format(@"C:\Temp\{0}.txt", nfiles)));
                    sb.AppendLine();
                    sb.AppendLine(String.Format("Write completed: {0}", string.Format(@"C:\Temp\{0}.txt", folders[0].name)));
                    richTextStatus.Text = sb.ToString();
                    richTextStatus.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Received an error. " + ex.Message);
            }
            finally
            {
                signinButton.Enabled  = true;
                signOutButton.Enabled = true;
                btnCreateMap.Enabled  = true;
                txtRoot.Enabled       = true;
            }
        }