예제 #1
0
        public override bool MoveResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry moveTo)
        {
            if (entry.Name.Equals("/"))
            {
                return(false);
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{destination: \"{0}\"}}", moveTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "MOVE", data, false);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                {
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                }
                var moveToBase = moveTo as BaseDirectoryEntry;
                if (moveToBase != null)
                {
                    moveToBase.AddChild(entry as BaseFileEntry);
                }
                return(true);
            }

            return(false);
        }
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            var url = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.DownloadUrlProperty);

            var format = GoogleDocsResourceHelper.GetExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));

            if (!String.IsNullOrEmpty(format))
            {
                url = String.Format("{0}&exportFormat={1}&format={1}", url, format);
            }

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

            MemoryStream stream = null;

            try
            {
                var response = request.GetResponse();
                using (var rs = response.GetResponseStream())
                {
                    stream = new MemoryStream();
                    rs.CopyTo(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                }
            }
            catch (WebException)
            {
            }

            return(stream);
        }
        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 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 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);
        }
예제 #6
0
        public override bool CopyResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry copyTo)
        {
            if (entry.Name.Equals("/"))
            {
                return(false);
            }

            if (entry is ICloudDirectoryEntry)
            {
                // skydrive allowes to copy only files so we will recursively create/copy entries
                var newEntry = CreateResource(session, entry.Name, copyTo) as ICloudDirectoryEntry;
                return(newEntry != null && (entry as ICloudDirectoryEntry).Aggregate(true, (current, subEntry) => current && CopyResource(session, subEntry, newEntry)));
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{destination: \"{0}\"}}", copyTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "COPY", data, false);

            if (json != null && !SkyDriveJsonParser.ContainsError(json, false))
            {
                var copyToBase = copyTo as BaseDirectoryEntry;
                if (copyToBase != null)
                {
                    copyToBase.AddChild(entry as BaseFileEntry);
                }
                return(true);
            }

            return(false);
        }
예제 #7
0
        public override bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "DELETE", null, true);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                {
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                }
                return(true);
            }

            return(false);
        }
예제 #8
0
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            //not refresh if resource was requested recently
            var timestamp     = resource.GetPropertyValue(SkyDriveConstants.TimestampKey);
            var refreshNeeded = DateTime.Parse(timestamp, CultureInfo.InvariantCulture) + TimeSpan.FromSeconds(5) < DateTime.UtcNow;

            if (refreshNeeded)
            {
                //Request resource by ID and then update properties from requested
                var current = RequestResource(session, resource.GetPropertyValue(SkyDriveConstants.InnerIDKey), null);
                SkyDriveHelpers.CopyProperties(current, resource);
            }

            var directory = resource as ICloudDirectoryEntry;

            if (directory != null && !refreshNeeded && directory.HasChildrens == nChildState.HasNotEvaluated)
            {
                RefreshDirectoryContent(session, directory);
            }
        }
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            var url = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.DownloadUrlProperty);

            var format = GoogleDocsResourceHelper.GetStreamExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));

            if (!String.IsNullOrEmpty(format))
            {
                url = String.Format("{0}&exportFormat={1}", url, format);
                if (format.Equals("docx"))
                {
                    url += "&format=" + format;
                }
            }

            var request  = CreateWebRequest(session, url, "GET", null, true);
            var response = (HttpWebResponse)request.GetResponse();

            if (fileSystemEntry.Length > 0)
            {
                return(new BaseFileEntryDownloadStream(response.GetResponseStream(), fileSystemEntry));
            }

            var isChukedEncoding = string.Equals(response.Headers.Get("Transfer-Encoding"), "Chunked", StringComparison.OrdinalIgnoreCase);

            if (!isChukedEncoding)
            {
                ((BaseFileEntry)fileSystemEntry).Length = response.ContentLength;
                return(new BaseFileEntryDownloadStream(response.GetResponseStream(), fileSystemEntry));
            }

            var tempBuffer = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 8096, FileOptions.DeleteOnClose);

            using (var stream = response.GetResponseStream())
            {
                stream.CopyTo(tempBuffer);
            }
            tempBuffer.Flush();
            tempBuffer.Seek(0, SeekOrigin.Begin);
            return(tempBuffer);
        }
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            if (resource == null)
            {
                return;
            }

            if (resource.Id.Equals(GoogleDocsConstants.RootFolderId))
            {
                RefreshDirectoryContent(session, resource as BaseDirectoryEntry);
                return;
            }

            var resourceUrl = String.Format(GoogleDocsConstants.GoogleDocsResourceUrlFormat, resource.Id.ReplaceFirst("_", "%3a"));
            var request     = CreateWebRequest(session, resourceUrl, "GET", null);

            request.Headers.Add("If-None-Match", resource.GetPropertyValue(GoogleDocsConstants.EtagProperty));

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode != HttpStatusCode.NotModified)
                    {
                        using (var s = response.GetResponseStream())
                            using (var streamReader = new StreamReader(s))
                            {
                                var xml = streamReader.ReadToEnd();

                                GoogleDocsResourceHelper.UpdateResourceByXml(session, out resource, xml);
                            }
                    }
                }

                var dirEntry = resource as BaseDirectoryEntry;

                if (dirEntry == null || dirEntry.HasChildrens == nChildState.HasChilds)
                {
                    return;
                }

                RefreshDirectoryContent(session, dirEntry);
            }
            catch (WebException)
            {
            }
        }
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            var path = GetResourceUrlInternal(session, DropBoxResourceIDHelpers.GetResourcePath(resource));

            int code;
            var res = DropBoxRequestParser.RequestResourceByUrl(path, this, session, out code);

            if (res.Length == 0)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotRetrieveDirectoryList);
            }

            // build the entry and subchilds
            DropBoxRequestParser.UpdateObjectFromJsonString(res, resource as BaseFileEntry, this, session);

            var hash = resource.GetPropertyValue("hash");

            if (!string.IsNullOrEmpty(hash))
            {
                DropBoxRequestParser.Addhash(path, hash, res, session);
            }
        }
예제 #12
0
        public override bool RenameResource(IStorageProviderSession session, ICloudFileSystemEntry entry, String newName)
        {
            if (entry.Name.Equals("/") || newName.Contains("/"))
            {
                return(false);
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{name: \"{0}\"}}", newName);
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "PUT", data, false);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var entryBase = entry as BaseFileEntry;
                if (entryBase != null)
                {
                    entryBase.Name = newName;
                }
                return(true);
            }

            return(false);
        }
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            var path = GetResourceUrlInternal(session, DropBoxResourceIDHelpers.GetResourcePath(resource));
         
            int code;
            var res = DropBoxRequestParser.RequestResourceByUrl(path, this, session, out code);

            if (res.Length == 0)
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotRetrieveDirectoryList);

            // build the entry and subchilds
            DropBoxRequestParser.UpdateObjectFromJsonString(res, resource as BaseFileEntry, this, session);

            var hash = resource.GetPropertyValue("hash");
            if (!string.IsNullOrEmpty(hash))
            {
                DropBoxRequestParser.Addhash(path, hash, res, session);
            }
        }
        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 Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry entry)
 {
     if (entry is ICloudDirectoryEntry)
         throw new ArgumentException("Download operation can be perform for files only");
     
     String uri = String.Format("{0}/{1}/content", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
     uri = SkyDriveRequestHelper.SignUri(session, uri);
     var request = WebRequest.Create(uri);
     var response = request.GetResponse();
     ((BaseFileEntry)entry).Length = response.ContentLength;
     return new BaseFileEntryDownloadStream(response.GetResponseStream(), entry);
 }
        public override bool CopyResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry copyTo)
        {
            if (entry.Name.Equals("/"))
                return false;

            if (entry is ICloudDirectoryEntry)
            {
                // skydrive allowes to copy only files so we will recursively create/copy entries
                var newEntry = CreateResource(session, entry.Name, copyTo) as ICloudDirectoryEntry;
                return newEntry != null && (entry as ICloudDirectoryEntry).Aggregate(true, (current, subEntry) => current && CopyResource(session, subEntry, newEntry));
            }

            String uri = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String data = String.Format("{{destination: \"{0}\"}}", copyTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String json = SkyDriveRequestHelper.PerformRequest(session, uri, "COPY", data, false);

            if (json != null && !SkyDriveJsonParser.ContainsError(json, false))
            {
                var copyToBase = copyTo as BaseDirectoryEntry;
                if (copyToBase != null)
                    copyToBase.AddChild(entry as BaseFileEntry);
                return true;
            }

            return false;
        }
        public override bool MoveResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry moveTo)
        {
            if (entry.Name.Equals("/"))
                return false;

            String uri = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String data = String.Format("{{destination: \"{0}\"}}", moveTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String json = SkyDriveRequestHelper.PerformRequest(session, uri, "MOVE", data, false);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                var moveToBase = moveTo as BaseDirectoryEntry;
                if (moveToBase != null)
                    moveToBase.AddChild(entry as BaseFileEntry);
                return true;
            }

            return false;
        }
        public override bool RenameResource(IStorageProviderSession session, ICloudFileSystemEntry entry, String newName)
        {
            if (entry.Name.Equals("/") || newName.Contains("/"))
                return false;

            String uri = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String data = String.Format("{{name: \"{0}\"}}", newName);
            String json = SkyDriveRequestHelper.PerformRequest(session, uri, "PUT", data, false);
            
            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var entryBase = entry as BaseFileEntry;
                if (entryBase != null)
                    entryBase.Name = newName;
                return true;
            }

            return false;
        }
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            var url = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.DownloadUrlProperty);

            var format = GoogleDocsResourceHelper.GetStreamExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));
            if (!String.IsNullOrEmpty(format))
            {
                url = String.Format("{0}&exportFormat={1}", url, format);
                if (format.Equals("docx"))
                    url += "&format=" + format;
            }

            var request = CreateWebRequest(session, url, "GET", null, true);
            var response = (HttpWebResponse)request.GetResponse();

            if (fileSystemEntry.Length > 0)
            {
                return new BaseFileEntryDownloadStream(response.GetResponseStream(), fileSystemEntry);
            }

            var isChukedEncoding = string.Equals(response.Headers.Get("Transfer-Encoding"), "Chunked", StringComparison.OrdinalIgnoreCase);
            if (!isChukedEncoding)
            {
                ((BaseFileEntry)fileSystemEntry).Length = response.ContentLength;
                return new BaseFileEntryDownloadStream(response.GetResponseStream(), fileSystemEntry);
            }

            var tempBuffer = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 8096, FileOptions.DeleteOnClose);
            response.GetResponseStream().CopyTo(tempBuffer);
            tempBuffer.Flush();
            tempBuffer.Seek(0, SeekOrigin.Begin);
            return tempBuffer;
        }
 public static bool OfGoogleDocsKind(ICloudFileSystemEntry entry)
 {
     var kind = entry.GetPropertyValue(GoogleDocsConstants.KindProperty);
     return kind.Equals("document") || kind.Equals("presentation") || kind.Equals("spreadsheet") || kind.Equals("drawing");
 }
예제 #21
0
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            if (entry is ICloudDirectoryEntry)
            {
                throw new ArgumentException("Download operation can be perform for files only");
            }

            var uri = String.Format("{0}/{1}/content", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));

            uri = SkyDriveRequestHelper.SignUri(session, uri);
            var request = WebRequest.Create(uri);

            using (var response = request.GetResponse())
            {
                ((BaseFileEntry)entry).Length = response.ContentLength;
                return(new BaseFileEntryDownloadStream(response.GetResponseStream(), entry));
            }
        }
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            if (resource == null)
                return;

            if (resource.Id.Equals(GoogleDocsConstants.RootFolderId))
            {
                RefreshDirectoryContent(session, resource as BaseDirectoryEntry);
                return;
            }

            var resourceUrl = String.Format(GoogleDocsConstants.GoogleDocsResourceUrlFormat, resource.Id.ReplaceFirst("_", "%3a"));
            var request = CreateWebRequest(session, resourceUrl, "GET", null);
            request.Headers.Add("If-None-Match", resource.GetPropertyValue(GoogleDocsConstants.EtagProperty));

            try
            {
                var response = (HttpWebResponse) request.GetResponse();

                if (response.StatusCode != HttpStatusCode.NotModified)
                {
                    var s = response.GetResponseStream();
                    var xml = new StreamReader(s).ReadToEnd();

                    GoogleDocsResourceHelper.UpdateResourceByXml(session, out resource, xml);
                }

                var dirEntry = resource as BaseDirectoryEntry;

                if (dirEntry == null || dirEntry.HasChildrens == nChildState.HasChilds)
                    return;

                RefreshDirectoryContent(session, dirEntry);
            }
            catch (WebException)
            {
                
            }
        }
        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 bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            String uri = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            String json = SkyDriveRequestHelper.PerformRequest(session, uri, "DELETE", null, true);
            
            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                return true;
            }

            return false;
        }
예제 #25
0
        public static bool OfGoogleDocsKind(ICloudFileSystemEntry entry)
        {
            var kind = entry.GetPropertyValue(GoogleDocsConstants.KindProperty);

            return(kind.Equals("document") || kind.Equals("presentation") || kind.Equals("spreadsheet") || kind.Equals("drawing"));
        }
        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 void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            //not refresh if resource was requested recently
            var timestamp = resource.GetPropertyValue(SkyDriveConstants.TimestampKey);
            var refreshNeeded = DateTime.Parse(timestamp, CultureInfo.InvariantCulture) + TimeSpan.FromSeconds(5) < DateTime.UtcNow;
            if (refreshNeeded)
            {
                //Request resource by ID and then update properties from requested
                var current = RequestResource(session, resource.GetPropertyValue(SkyDriveConstants.InnerIDKey), null);
                SkyDriveHelpers.CopyProperties(current, resource);
            }

            var directory = resource as ICloudDirectoryEntry;
            if (directory != null && !refreshNeeded && directory.HasChildrens == nChildState.HasNotEvaluated)
                RefreshDirectoryContent(session, directory);
        }
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            var url = fileSystemEntry.GetPropertyValue(GoogleDocsConstants.DownloadUrlProperty);
          
            var format = GoogleDocsResourceHelper.GetExtensionByKind(fileSystemEntry.GetPropertyValue(GoogleDocsConstants.KindProperty));
            if (!String.IsNullOrEmpty(format))
            {
                url = String.Format("{0}&exportFormat={1}&format={1}", url, format);
            }

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

            MemoryStream stream = null;
            try
            {
                var response = request.GetResponse();
                using (var rs = response.GetResponseStream())
                {
                    stream = new MemoryStream();
                    rs.CopyTo(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                }
            }
            catch (WebException)
            {
            }

            return stream;
        }