public override IResumableUploadSession CreateUploadSession(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long bytesToTransfer)
        {
            WebRequest request;

            if (GoogleDocsResourceHelper.IsResorceId(fileSystemEntry.Id))
            {
                //request for update
                request = CreateWebRequest(session, fileSystemEntry.GetPropertyValue(GoogleDocsConstants.ResEditMediaProperty), "PUT", null, true);
                request.Headers.Add("If-Match", "*");
            }
            else
            {
                //request for create
                request = CreateWebRequest(session, fileSystemEntry.Parent.GetPropertyValue(GoogleDocsConstants.ResCreateMediaProperty) + "?convert=false", "POST", null, true);
            }

            if (GoogleDocsResourceHelper.OfGoogleDocsKind(fileSystemEntry))
            {
                ((BaseFileEntry)fileSystemEntry).Name = Path.GetFileNameWithoutExtension(fileSystemEntry.Name);
            }

            GoogleDocsXmlParser.WriteAtom(request, GoogleDocsXmlParser.EntryElement(GoogleDocsXmlParser.TitleElement(fileSystemEntry.Name)));
            request.Headers.Add("X-Upload-Content-Type", Common.Net.MimeMapping.GetMimeMapping(fileSystemEntry.Name));
            request.Headers.Add("X-Upload-Content-Length", bytesToTransfer.ToString(CultureInfo.InvariantCulture));


            var response = request.GetResponse();

            var uploadSession = new ResumableUploadSession(fileSystemEntry, bytesToTransfer);

            uploadSession["Location"] = response.Headers["Location"];
            uploadSession.Status      = ResumableUploadSessionStatus.Started;

            return(uploadSession);
        }
        public override bool RenameResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, string newName)
        {
            if (String.IsNullOrEmpty(newName) || GoogleDocsResourceHelper.IsNoolOrRoot(fsentry))
            {
                return(false);
            }

            var url = String.Format(GoogleDocsConstants.GoogleDocsResourceUrlFormat, fsentry.Id.ReplaceFirst("_", "%3a"));

            var request = CreateWebRequest(session, url, "PUT", null);

            request.Headers.Add("If-Match", fsentry.GetPropertyValue(GoogleDocsConstants.EtagProperty));
            GoogleDocsXmlParser.WriteAtom(request, GoogleDocsXmlParser.EntryElement(GoogleDocsXmlParser.TitleElement(newName)));

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //check if extension added
                    if (!(fsentry is ICloudDirectoryEntry) && String.IsNullOrEmpty(Path.GetExtension(newName)))
                    {
                        newName = newName + Path.GetExtension(fsentry.Name);
                    }
                    (fsentry as BaseFileEntry).Name = newName;
                    return(true);
                }
            }
            catch (WebException)
            {
            }

            return(false);
        }
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
        {
            String url;
            bool   update = false;

            String type = GetMimeType(fileSystemEntry.Name);

            if (GoogleDocsResourceHelper.IsResorceId(fileSystemEntry.Id)) //update existent
            {
                var ext = GoogleDocsResourceHelper.GetExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));
                if (!String.IsNullOrEmpty(ext))
                {
                    //for google docs kind we previously add an extension that don't needed anymore
                    var index = fileSystemEntry.Name.IndexOf('.' + ext, StringComparison.Ordinal);
                    (fileSystemEntry as BaseFileEntry).Name = fileSystemEntry.Name.Substring(0, index);
                }

                url    = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.ResEditMediaProperty);
                update = true;

                if (String.IsNullOrEmpty(url))
                {
                    throw new HttpException(403, "User not authorize to update resource");
                }
            }
            else
            {
                url = fileSystemEntry.Parent.GetPropertyValue(GoogleDocsConstants.ResCreateMediaProperty) + "?convert=false";
            }

            //first initiate resumable upload request
            var initRequest = CreateWebRequest(session, url, update ? "PUT" : "POST", null);

            initRequest.Headers.Add("X-Upload-Content-Length", uploadSize.ToString(CultureInfo.InvariantCulture));
            initRequest.Headers.Add("X-Upload-Content-Type", type);
            if (update)
            {
                initRequest.Headers.Add("If-Match", "*");
            }
            GoogleDocsXmlParser.WriteAtom(initRequest, GoogleDocsXmlParser.EntryElement(null, GoogleDocsXmlParser.TitleElement(fileSystemEntry.Name)));

            var response = initRequest.GetResponse();

            //secondly create request to obtained url
            var uplRequest = CreateWebRequest(session, response.Headers["Location"], "PUT", null);

            uplRequest.ContentLength = uploadSize;
            uplRequest.ContentType   = type;
            uplRequest.Headers.Add("Content-Range", String.Format("bytes {0}-{1}/{2}", 0, uploadSize - 1, uploadSize));

            var wrStream = new WebRequestStream(uplRequest.GetRequestStream(), uplRequest, null);

            wrStream.PushPostDisposeOperation(CommitUploadOperation, session, uplRequest, fileSystemEntry);

            return(wrStream);
        }
        public override ICloudFileSystemEntry CreateResource(IStorageProviderSession session, string name, ICloudDirectoryEntry parent)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name cannot be empty");
            }

            var url = GoogleDocsResourceHelper.IsNoolOrRoot(parent)
                          ? GoogleDocsConstants.GoogleDocsFeedUrl
                          : String.Format(GoogleDocsConstants.GoogleDocsContentsUrlFormat, parent.Id.ReplaceFirst("_", "%3a"));

            var request = CreateWebRequest(session, url, "POST", null);

            GoogleDocsXmlParser.WriteAtom(request, GoogleDocsXmlParser.EntryElement(GoogleDocsXmlParser.CategoryElement(), GoogleDocsXmlParser.TitleElement(name)));

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        using (var rs = response.GetResponseStream())
                            using (var streamReader = new StreamReader(rs))
                            {
                                var xml   = streamReader.ReadToEnd();
                                var entry = GoogleDocsXmlParser.ParseEntriesXml(session, xml).First();

                                if (parent != null)
                                {
                                    (parent as BaseDirectoryEntry).AddChild(entry);
                                }

                                return(entry);
                            }
                    }
                }
            }
            catch (WebException)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorCreateOperationFailed);
            }

            return(null);
        }
        public bool AddToCollection(IStorageProviderSession session, ICloudFileSystemEntry resource, ICloudDirectoryEntry collection)
        {
            var url     = String.Format(GoogleDocsConstants.GoogleDocsContentsUrlFormat, collection.Id.ReplaceFirst("_", "%3a"));
            var request = CreateWebRequest(session, url, "POST", null);

            GoogleDocsXmlParser.WriteAtom(request, GoogleDocsXmlParser.EntryElement(null, GoogleDocsXmlParser.IdElement(resource.Id.ReplaceFirst("_", "%3a"))));

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.Created)
                {
                    return(true);
                }
            }
            catch (WebException)
            {
            }

            return(false);
        }