示例#1
0
        private GoogleDriveFolderItem CreateFolder(string name, string parent)
        {
            var url    = string.Format("{0}/files", DRIVE_API_URL);
            var folder = new GoogleDriveFolderItem()
            {
                title       = name,
                description = name,
                mimeType    = FOLDER_MIMETYPE,
                labels      = new GoogleDriveFolderItemLabels {
                    hidden = true
                },
                parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference()
                                                             {
                                                                 id = parent
                                                             } }
            };

            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(folder));

            return(m_oauth.GetJSONData <GoogleDriveFolderItem>(url, x =>
            {
                x.Method = "POST";
                x.ContentType = "application/json; charset=UTF-8";
                x.ContentLength = data.Length;
            }, req => {
                using (var rs = req.GetRequestStream())
                    rs.Write(data, 0, data.Length);
            }));
        }
示例#2
0
        public void Put(string remotename, System.IO.Stream stream)
        {
            try
            {
                // Figure out if we update or create the file
                if (m_filecache.Count == 0)
                {
                    foreach (var file in List()) /* Enumerate the full listing */ } {
            }

            GoogleDriveFolderItem[] files;
            m_filecache.TryGetValue(remotename, out files);

            string fileId = null;
            if (files != null)
            {
                if (files.Length == 1)
                {
                    fileId = files[0].id;
                }
                else
                {
                    Delete(remotename);
                }
            }

            var isUpdate = !string.IsNullOrWhiteSpace(fileId);

            var values = new NameValueCollection {
                { WebApi.GoogleDrive.QueryParam.UploadType,
                  WebApi.GoogleDrive.QueryValue.Resumable }
            };

            var url = isUpdate ?
                      WebApi.GoogleDrive.FileUploadUrl(Library.Utility.Uri.UrlPathEncode(fileId), values) :
                      WebApi.GoogleDrive.FileUploadUrl(values);

            var item = new GoogleDriveFolderItem
            {
                title       = remotename,
                description = remotename,
                mimeType    = "application/octet-stream",
                labels      = new GoogleDriveFolderItemLabels {
                    hidden = true
                },
                parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference {
                                                                 id = CurrentFolderId
                                                             } }
            };

            var res = GoogleCommon.ChunckedUploadWithResume <GoogleDriveFolderItem, GoogleDriveFolderItem>(m_oauth, item, url, stream, isUpdate ? "PUT" : "POST");
            m_filecache[remotename] = new GoogleDriveFolderItem[] { res };
        }
        catch
        {
            m_filecache.Clear();
            throw;
        }
    }
示例#3
0
        public void Put(string remotename, System.IO.Stream stream)
        {
            try
            {
                // Figure out if we update or create the file
                if (m_filecache.Count == 0)
                {
                    List();
                }

                GoogleDriveFolderItem[] files;
                m_filecache.TryGetValue(remotename, out files);

                string fileid = null;
                if (files != null)
                {
                    if (files.Length == 1)
                    {
                        fileid = files[0].id;
                    }
                    else
                    {
                        Delete(remotename);
                    }
                }

                var isUpdate = !string.IsNullOrWhiteSpace(fileid);

                var url = isUpdate ?
                          string.Format("{0}/files/{1}?uploadType=resumable", DRIVE_API_UPLOAD_URL, Library.Utility.Uri.UrlPathEncode(fileid)) :
                          string.Format("{0}/files?uploadType=resumable", DRIVE_API_UPLOAD_URL);

                var item = new GoogleDriveFolderItem()
                {
                    title       = remotename,
                    description = remotename,
                    mimeType    = "application/octet-stream",
                    labels      = new GoogleDriveFolderItemLabels {
                        hidden = true
                    },
                    parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference()
                                                                 {
                                                                     id = CurrentFolderId
                                                                 } }
                };

                var res = GoogleCommon.ChunckedUploadWithResume <GoogleDriveFolderItem, GoogleDriveFolderItem>(m_oauth, item, url, stream, isUpdate ? "PUT" : "POST");
                m_filecache[remotename] = new GoogleDriveFolderItem[] { res };
            }
            catch
            {
                m_filecache.Clear();
                throw;
            }
        }
示例#4
0
        public async Task PutAsync(string remotename, System.IO.Stream stream, CancellationToken cancelToken)
        {
            try
            {
                // Figure out if we update or create the file
                if (m_filecache.Count == 0)
                {
                    foreach (var file in List()) /* Enumerate the full listing */ } {
            }

            GoogleDriveFolderItem[] files;
            m_filecache.TryGetValue(remotename, out files);

            string fileId = null;
            if (files != null)
            {
                if (files.Length == 1)
                {
                    fileId = files[0].id;
                }
                else
                {
                    Delete(remotename);
                }
            }

            var isUpdate = !string.IsNullOrWhiteSpace(fileId);

            var url = WebApi.GoogleDrive.PutUrl(fileId, m_teamDriveID != null);

            var item = new GoogleDriveFolderItem
            {
                title       = remotename,
                description = remotename,
                mimeType    = "application/octet-stream",
                labels      = new GoogleDriveFolderItemLabels {
                    hidden = true
                },
                parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference {
                                                                 id = CurrentFolderId
                                                             } },
                teamDriveId = m_teamDriveID
            };

            var res = await GoogleCommon.ChunkedUploadWithResumeAsync <GoogleDriveFolderItem, GoogleDriveFolderItem>(m_oauth, item, url, stream, cancelToken, isUpdate? "PUT" : "POST");

            m_filecache[remotename] = new GoogleDriveFolderItem[] { res };
        }
        catch
        {
            m_filecache.Clear();
            throw;
        }
    }
示例#5
0
public void Rename(string oldname, string newname)
{
    try
    {
        var files = GetFileEntries(oldname, true);
        if (files.Length > 1)
        {
            throw new UserInformationException(string.Format(Strings.GoogleDrive.MultipleEntries(oldname, m_path)),
                                               "GoogleDriveMultipleEntries");
        }

        var newfile = JsonConvert.DeserializeObject <GoogleDriveFolderItem>(JsonConvert.SerializeObject(files[0]));
        newfile.title   = newname;
        newfile.parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference {
                                                                 id = CurrentFolderId
                                                             } };

        var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(newfile));

        var nf = m_oauth.GetJSONData <GoogleDriveFolderItem>(WebApi.GoogleDrive.GetUrl(files[0].id), x =>
                {
                    x.Method        = "PUT";
                    x.ContentLength = data.Length;
                    x.ContentType   = "application/json; charset=UTF-8";
                }, x =>
                {
                    using (var rs = x.GetRequestStream())
                        rs.Write(data, 0, data.Length);
                });

        m_filecache[newname] = new GoogleDriveFolderItem[] { nf };
        m_filecache.Remove(oldname);
    }
    catch
    {
        m_filecache.Clear();

        throw;
    }
}
示例#6
0

        
示例#7
0
        private GoogleDriveFolderItem CreateFolder(string name, string parent)
        {
            var url = string.Format("{0}/files", DRIVE_API_URL);
            var folder = new GoogleDriveFolderItem() {
                title = name,
                description = name,
                mimeType = FOLDER_MIMETYPE,
                labels = new GoogleDriveFolderItemLabels { hidden = true },
                parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference() { id = parent } }
            };

            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(folder));

            return m_oauth.GetJSONData<GoogleDriveFolderItem>(url, x =>
            {
                x.Method = "POST";
                x.ContentType = "application/json; charset=UTF-8";
                x.ContentLength = data.Length;

            }, req => {
                
                using(var rs = req.GetRequestStream())
                    rs.Write(data, 0, data.Length);
                
            });

        }
示例#8
0
        public void Rename(string oldname, string newname)
        {
            try
            {
                var files = GetFileEntries(oldname, true);
                if (files.Length > 1)
                    throw new Exception(string.Format(Strings.GoogleDrive.MultipleEntries(oldname, m_path)));

                var newfile = JsonConvert.DeserializeObject<GoogleDriveFolderItem>(JsonConvert.SerializeObject(files[0]));
                newfile.title = newname;
                newfile.parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference() { id = CurrentFolderId } };

                var url = string.Format("{0}/files/{1}", DRIVE_API_UPLOAD_URL, Library.Utility.Uri.UrlPathEncode(files[0].id));
                var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(newfile));

                var nf = m_oauth.GetJSONData<GoogleDriveFolderItem>(url, x => {
                    x.Method = "PUT";
                    x.ContentLength = data.Length;
                    x.ContentType = "application/json; charset=UTF-8";
                }, x => {
                    
                    using(var rs = x.GetRequestStream())
                        rs.Write(data, 0, data.Length);
                });
                    
                m_filecache[newname] = new GoogleDriveFolderItem[] { nf };
                m_filecache.Remove(oldname);
            }
            catch
            {
                m_filecache.Clear();

                throw;
            }

        }
示例#9
0
        public void Put(string remotename, System.IO.Stream stream)
        {
            try
            {
                // Figure out if we update or create the file
                if (m_filecache.Count == 0)
                    List();

                GoogleDriveFolderItem[] files;
                m_filecache.TryGetValue(remotename, out files);

                string fileid = null;
                if (files != null)
                {
                    if (files.Length == 1)
                        fileid = files[0].id;
                    else
                        Delete(remotename);
                }

                var isUpdate = !string.IsNullOrWhiteSpace(fileid);

                var url = isUpdate ?
                    string.Format("{0}/files/{1}?uploadType=resumable", DRIVE_API_UPLOAD_URL, Library.Utility.Uri.UrlPathEncode(fileid)) :
                    string.Format("{0}/files?uploadType=resumable", DRIVE_API_UPLOAD_URL);

                var item = new GoogleDriveFolderItem() {
                    title = remotename,
                    description = remotename,
                    mimeType = "application/octet-stream",
                    labels = new GoogleDriveFolderItemLabels { hidden = true },
                    parents = new GoogleDriveParentReference[] { new GoogleDriveParentReference() { id = CurrentFolderId } }
                };

                var res = GoogleCommon.ChunckedUploadWithResume<GoogleDriveFolderItem, GoogleDriveFolderItem>(m_oauth, item, url, stream, isUpdate ? "PUT" : "POST");
                m_filecache[remotename] = new GoogleDriveFolderItem[] { res };
            }
            catch
            {
                m_filecache.Clear();
                throw;
            }

        }