Exemplo n.º 1
0
        public static void CreateFolder(IItemNode node)
        {
            string     Email     = node.GetRoot.RootType.Email;
            DriveAPIv2 gdclient  = GetAPIv2(Email);
            string     parent_id = "";

            try
            {
                List <IItemNode> listnode = node.GetFullPath();
                Monitor.Enter(sync_createfolder);
                int i;
                for (i = listnode.Count - 1; i > 0; i--)
                {
                    if (!string.IsNullOrEmpty(listnode[i].Info.ID))
                    {
                        parent_id = listnode[i].Info.ID; break;
                    }
                }
                i++;
                bool create = false;
                for (; i < listnode.Count; i++)
                {
                    if (!create)
                    {
                        List <IItemNode> listsearchnode = Search("'" + parent_id + "' in parents" +
                                                                 " and trashed=false" +
                                                                 " and title='" + listnode[i].Info.Name.Replace("'", "\\'") + "'" +
                                                                 " and mimeType = 'application/vnd.google-apps.folder'", Email).Convert(node);
                        if (listsearchnode.Count == 0)
                        {
                            create = true;
                        }
                        else
                        {
                            parent_id = listnode[i].Info.ID = listsearchnode[0].Info.ID;
                        }
                    }

                    if (create)
                    {
                        Drivev2_File metadata = new Drivev2_File()
                        {
                            title = listnode[i].Info.Name
                        };
                        metadata.parents = new List <Drivev2_Parent>()
                        {
                            new Drivev2_Parent()
                            {
                                id = parent_id
                            }
                        };

                        Drivev2_File folder = gdclient.Extend.CreateFolder(metadata);
                        parent_id = listnode[i].Info.ID = folder.id;
                    }
                }
            }
            finally { Monitor.Exit(sync_createfolder); }
        }
Exemplo n.º 2
0
        static void TestDrive2Copy()
        {
            //file 0Bx154iMNwuyWUUJEUTNRMnAwc0k           folder 0B2T-102UejylQmwxSnFGN3RsLWM
            DriveAPIv2   v2 = GetAPIv2(email_gd);
            Drivev2_File f  = v2.Files.Copy("0Bx154iMNwuyWUUJEUTNRMnAwc0k", "0B2T-102UejylQmwxSnFGN3RsLWM");

            return;
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="nodemove">Item move</param>
        /// <param name="newparent">if cut then parent, copy then item</param>
        /// <param name="newname">rename (cut)</param>
        /// <param name="copy"></param>
        /// <returns></returns>
        public static Drivev2_File MoveItem(IItemNode nodemove, IItemNode newparent, string newname = null, bool copy = false)
        {
            if (newparent != null && nodemove.GetRoot.RootType.Type != newparent.GetRoot.RootType.Type)
            {
                throw new Exception("TypeCloud not match.");
            }
            DriveAPIv2 gdclient;

            if (!copy)
            {
                //Same account
                gdclient = GetAPIv2(nodemove.GetRoot.RootType.Email);
                if (newparent != null && nodemove.GetRoot.RootType.Email != newparent.GetRoot.RootType.Email)
                {
                    throw new Exception("Email not match.");
                }
                Drivev2_File item = null;
                if (newparent == null & newname != null)//rename
                {
                    item       = new Drivev2_File();
                    item.title = newname;
                }
                else//move
                {
                    Drivev2_Parents_list parents = gdclient.Parent.List(nodemove.Info.ID);
                    Drivev2_Parent       found   = parents.items.Find(p => p.id == nodemove.Parent.Info.ID);
                    if (found != null)
                    {
                        parents.items.Remove(found);
                    }
                    parents.items.Add(new Drivev2_Parent()
                    {
                        id = newparent.Info.ID
                    });
                    gdclient.Parent.Insert(nodemove.Info.ID, parents.items);
                }
                return(gdclient.Files.Patch(nodemove.Info.ID, item));
            }
            else
            {
                CreateFolder(newparent.Parent);
                if (string.IsNullOrEmpty(newparent.Parent.Info.ID))
                {
                    Console.WriteLine("error");
                }
                return(GetAPIv2(newparent.GetRoot.RootType.Email).Files.Copy(nodemove.Info.ID, newparent.Parent.Info.ID));
            }
        }
Exemplo n.º 4
0
        //Rename: MoveItem(node,null,newname,false)
        //Cut: MoveItem(node,newparent,null,false)
        //Copy: MoveItem(node,newparent,null,true)
        //
        public bool MoveItem(IItemNode node, IItemNode newparent = null, string newname = null, bool Copy = false)
        {
            CheckThread(false);
            if ((newparent != null && node.GetRoot == newparent.GetRoot) | newparent == null)
            {
                bool flag = false;
                switch (node.GetRoot.RootType.Type)
                {
                case CloudType.Dropbox: flag = Dropbox.Move(node, newparent, newname); break;

                case CloudType.GoogleDrive:
                    Drivev2_File item = GoogleDrive.MoveItem(node, newparent, newname);
                    if (item.title == newname)
                    {
                        flag = true;
                    }
                    item.parents.ForEach(s => { if (!flag && newparent != null && s.id == newparent.Info.ID)
                                                {
                                                    flag = true;
                                                }
                                         });
                    break;

                case CloudType.LocalDisk: flag = LocalDisk.Move(node, newparent, newname); break;

                case CloudType.Mega:
                default: throw new Exception("CloudType not support (" + node.GetRoot.RootType.Type.ToString() + ").");
                }
                if (flag)
                {
                    if (newparent != null)
                    {
                        node.Parent.RemoveChild(node);
                        newparent.AddChild(node);
                    }
                    else
                    {
                        node.Info.Name = newname;
                    }
                }
                return(flag);
            }
            else
            {
                throw new Exception("Root not match.");
            }
        }
Exemplo n.º 5
0
        public static bool ReNameItem(IItemNode node, string newname)
        {
            DriveAPIv2   gdclient = GetAPIv2(node.GetRoot.RootType.Email);
            Drivev2_File response = gdclient.Files.Patch(node.Info.ID, new Drivev2_File()
            {
                title = newname
            });

            if (response.title == newname)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <param name="import">False is copy</param>
        void SameAccountCloud(TransferItem item, bool import = true)
        {
            switch (item.From.node.GetRoot.RootType.Type)
            {
            case CloudType.GoogleDrive:
                Drivev2_File f = GoogleDrive.MoveItem(item.From.node, item.To.node, null, true);

                foreach (var p in f.parents)
                {
                    if (p.id == item.To.node.Parent.Info.ID)
                    {
                        item.status = StatusTransfer.Done;
                    }
                    else
                    {
                        item.status = StatusTransfer.Error;
                    }
                }
                break;

            default: throw new Exception("SameAccountCloud not support now.");
            }
        }
Exemplo n.º 7
0
        public IItemNode GetFileInfo(IItemNode node)
        {
            switch (node.GetRoot.RootType.Type)
            {
            case CloudType.Dropbox:
                return(Dropbox.GetMetaData(node));

            case CloudType.GoogleDrive:
                Drivev2_File item = GoogleDrive.GetMetadataItem(node);
                node.Info.Size    = item.fileSize ?? -1;
                node.Info.Name    = item.title;
                node.Info.DateMod = item.modifiedDate ?? DateTime.Now;
                return(node);

            case CloudType.LocalDisk:
                return(LocalDisk.GetFileInfo(node));

            case CloudType.Mega:
                return(MegaNz.GetItem(node));

            default:
                throw new UnknowCloudNameException("Error Unknow Cloud Type: " + node.GetRoot.RootType.Type.ToString());
            }
        }
Exemplo n.º 8
0
        void Transfer(TransferItem item)
        {
#if DEBUG
            Console.WriteLine("Transfer items:" + item.From.node.GetFullPathString());
#endif
            int buffer_length = 32;                                                                                                                 //default
            int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.BufferSize), out buffer_length);                                       //get buffer_length from setting
            item.buffer = item.From.node.GetRoot.RootType.Type == CloudType.Mega ? new byte[buffer_length * 2048] : new byte[buffer_length * 1024]; //create buffer

            ItemNode rootnodeto = item.To.node.GetRoot;

            item.byteread = 0;
            //this.group.items[x].UploadID = "";//resume
            item.SizeWasTransfer  = item.OldTransfer = item.SaveSizeTransferSuccess; //resume
            item.DataSource.Error = string.Empty;                                    //clear error
            item.TimeStamp        = CurrentMillis.Millis;
            switch (rootnodeto.GetRoot.RootType.Type)
            {
            case CloudType.LocalDisk:
                #region LocalDisk
                ItemsTransferWork.Add(new TransferBytes(item, this));
                return;

                #endregion

            case CloudType.Dropbox:
                #region Dropbox
                int chunksizedb = 25;//default 25Mb
                int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.Dropbox_ChunksSize), out chunksizedb);
                item.ChunkUploadSize = chunksizedb * 1024 * 1024;

                DropboxRequestAPIv2 DropboxClient = Dropbox.GetAPIv2(rootnodeto.GetRoot.RootType.Email);

                if (string.IsNullOrEmpty(item.UploadID))//create upload id
                {
                    item.byteread = item.From.stream.Read(item.buffer, 0, item.buffer.Length);
                    IDropbox_Request_UploadSessionAppend session = DropboxClient.upload_session_start(item.buffer, item.byteread);
                    item.UploadID         = session.session_id;
                    item.SizeWasTransfer += item.byteread;
                }
                ItemsTransferWork.Add(new TransferBytes(item, DropboxClient));
                return;

                #endregion

            case CloudType.GoogleDrive:
                #region GoogleDrive
                DriveAPIv2 gdclient = GoogleDrive.GetAPIv2(rootnodeto.GetRoot.RootType.Email);
                GoogleDrive.CreateFolder(item.To.node.Parent);
                int chunksizeGD = 5;//default
                int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.GD_ChunksSize), out chunksizeGD);
                item.ChunkUploadSize = chunksizeGD * 1024 * 1024;

                if (string.IsNullOrEmpty(item.UploadID))//create upload id
                {
                    if (string.IsNullOrEmpty(item.To.node.Parent.Info.ID))
                    {
                        throw new Exception("Can't get root id.");
                    }
                    string parentid = item.To.node.Parent.Info.ID;
                    string mimeType = Get_mimeType.Get_mimeType_From_FileExtension(item.To.node.GetExtension());

                    Drivev2_File f_metadata = new Drivev2_File()
                    {
                        title = item.From.node.Info.Name, mimeType = mimeType
                    };
                    f_metadata.parents = new List <Drivev2_Parent>()
                    {
                        new Drivev2_Parent()
                        {
                            id = parentid
                        }
                    };

                    item.UploadID = gdclient.Files.Insert_Resumable_GetUploadID(f_metadata, mimeType, item.From.node.Info.Size);
                }
                ItemsTransferWork.Add(new TransferBytes(item, gdclient));
                return;

                #endregion

            case CloudType.Mega:
                #region Mega
                MegaApiClient MegaClient = MegaNz.GetClient(rootnodeto.GetRoot.RootType.Email);
                item.buffer = new byte[128 * 1024];
                if (string.IsNullOrEmpty(item.UploadID))                                                                                      //create upload id
                {
                    MegaNz.AutoCreateFolder(item.To.node.Parent);                                                                             //auto create folder
                    item.UploadID = MegaClient.RequestUrlUpload(item.From.node.Info.Size);                                                    //Make Upload url
                }
                item.From.stream = MegaApiClient.MakeEncryptStreamForUpload(item.From.stream, item.From.node.Info.Size, item.dataCryptoMega); //make encrypt stream from file
                ItemsTransferWork.Add(new TransferBytes(item, MegaClient));
                return;

                #endregion
            }
        }
Exemplo n.º 9
0
        public static IItemNode GetListFileFolder(IItemNode node, bool folderonly = false, bool read_only = false)
        {
            bool     uri       = false;
            RootNode root      = node.GetRoot;
            string   Email     = root.RootType.Email;
            string   parent_ID = null;
            string   url       = null;
            Regex    rg;
            Match    match;

            if (string.IsNullOrEmpty(Email))
            {
                Email = AppSetting.settings.GetDefaultCloud(CloudType.GoogleDrive); uri = true;
            }

            #region Get parent_ID
            if (uri)//folder url
            {
                if (root.RootType.uri != null)
                {
                    url   = root.RootType.uri.ToString();
                    rg    = new Regex(Rg_url_idFolder);
                    match = rg.Match(url);
                    if (match.Success)
                    {
                        parent_ID = match.Value;
                    }
                    else
                    {
                        rg    = new Regex(Rg_url_idFolderOpen);
                        match = rg.Match(url);
                        if (match.Success)
                        {
                            parent_ID = match.Value;
                        }
                    }
                }
            }
            else//explorer node
            {
                parent_ID = "root";//root
                if (!string.IsNullOrEmpty(node.Info.ID))
                {
                    parent_ID = node.Info.ID;                             //id root or id node
                }
            }
            #endregion

            #region Get Child Node Data
            if (!string.IsNullOrEmpty(parent_ID))//if found id is folder
            {
                List <Drivev2_File> list_ = Search("'" + parent_ID + "' in parents and trashed=false", Email);
                if (parent_ID == "root")//save root id
                {
                    foreach (Drivev2_File item in list_)
                    {
                        foreach (Drivev2_Parent parent in item.parents)
                        {
                            if (parent.isRoot ?? false)
                            {
                                parent_ID = parent.id; break;
                            }
                        }
                        if (parent_ID != "root")
                        {
                            break;
                        }
                    }
                    node.Info.ID = parent_ID;
                    AppSetting.settings.SetRootID(Email, CloudType.GoogleDrive, parent_ID);
                }
                node.RenewChilds(list_.Convert(node));
                return(node);
            }
            else if (string.IsNullOrEmpty(url))//if id from url
            {
                rg    = new Regex(Rg_url_idFile);
                match = rg.Match(url);
                if (match.Success)
                {
                    RootNode n = new RootNode();
                    n.Info.ID        = match.Value;
                    n.RootType.Email = Email;
                    Drivev2_File item = GoogleDrive.GetMetadataItem(n);
                    n.Info.Size     = item.fileSize ?? -1;
                    n.Info.Name     = item.title;
                    n.Info.MimeType = item.mimeType;
                    AppSetting.UIMain.FileSaveDialog(PCPath.Mycomputer, item.title, PCPath.FilterAllFiles, n);
                    return(null);
                }
            }
            #endregion

            throw new Exception("Can't Analyze Data Input.");
        }