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 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 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 ICloudFileSystemEntry RequestResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent) { if (GoogleDocsResourceHelper.IsNoolOrRoot(parent) && GoogleDocsResourceHelper.IsRootName(Name)) { var root = new BaseDirectoryEntry("/", 0, DateTime.Now, session.Service, session) { Id = GoogleDocsConstants.RootFolderId }; root.SetPropertyValue(GoogleDocsConstants.ResCreateMediaProperty, GoogleDocsConstants.RootResCreateMediaUrl); RefreshDirectoryContent(session, root); return(root); } if (Name.Equals("/")) { RefreshDirectoryContent(session, parent as BaseDirectoryEntry); } if (GoogleDocsResourceHelper.IsResorceId(Name)) { var url = String.Format(GoogleDocsConstants.GoogleDocsResourceUrlFormat, Name.ReplaceFirst("_", "%3a")); var request = CreateWebRequest(session, url, "GET", null); try { var response = (HttpWebResponse)request.GetResponse(); var rs = response.GetResponseStream(); var xml = new StreamReader(rs).ReadToEnd(); var entry = GoogleDocsXmlParser.ParseEntriesXml(session, xml).FirstOrDefault(); if (entry == null) { throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound); } if (parent != null) { (parent as BaseDirectoryEntry).AddChild(entry); } var dirEntry = entry as BaseDirectoryEntry; if (dirEntry == null) { return(entry); } RefreshDirectoryContent(session, dirEntry); return(dirEntry); } catch (WebException) { throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound); } } throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName); }
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 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 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 override bool MoveResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, ICloudDirectoryEntry newParent) { if (fsentry == null || newParent == null || GoogleDocsResourceHelper.IsNoolOrRoot(fsentry)) { return(false); } if (RemoveResource(session, fsentry, RemoveMode.FromParentCollection) && AddToCollection(session, fsentry, newParent)) { if (fsentry.Parent != null) { (fsentry.Parent as BaseDirectoryEntry).RemoveChild(fsentry as BaseFileEntry); } (newParent as BaseDirectoryEntry).AddChild(fsentry as BaseFileEntry); return(true); } return(false); }
public override bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry) { if (GoogleDocsResourceHelper.IsNoolOrRoot(entry)) { return(false); } if (RemoveResource(session, entry, RemoveMode.Delete)) { (entry as BaseFileEntry).IsDeleted = true; if (entry.Parent != null) { (entry.Parent as BaseDirectoryEntry).RemoveChildById(entry.Id); } 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); using (var stream = response.GetResponseStream()) { stream.CopyTo(tempBuffer); } tempBuffer.Flush(); tempBuffer.Seek(0, SeekOrigin.Begin); return(tempBuffer); }
public override string GetResourceUrl(IStorageProviderSession session, ICloudFileSystemEntry entry, string path) { if (!String.IsNullOrEmpty(path)) { var id = path; var index = id.LastIndexOf("/"); if (index != -1) { id = id.Substring(index + 1); } if (GoogleDocsResourceHelper.IsResorceId(id)) { return(id); } } else if (entry != null) { return(entry.Id); } return(base.GetResourceUrl(session, null, path)); }
public static IEnumerable <BaseFileEntry> ParseEntriesXml(IStorageProviderSession session, String xml) { var doc = XDocument.Load(new StringReader(xml)); var entries = doc.Descendants(XName.Get("entry", GoogleDocsConstants.AtomNamespace)); var fsEntries = new List <BaseFileEntry>(); foreach (var entry in entries) { var resourceId = entry.Element(XName.Get("resourceId", GoogleDocsConstants.GdNamespace)).ValueOrEmpty().Replace(':', '_'); var title = entry.Element(XName.Get("title", GoogleDocsConstants.AtomNamespace)).ValueOrEmpty(); var updated = entry.Element(XName.Get("updated", GoogleDocsConstants.AtomNamespace)).ValueOrEmpty(); var etag = entry.Attribute(XName.Get("etag", GoogleDocsConstants.GdNamespace)).ValueOrEmpty(); var kind = entry.Elements(XName.Get("category", GoogleDocsConstants.AtomNamespace)).Single(x => x.Attribute("scheme").ValueOrEmpty().Equals(GoogleDocsConstants.SchemeKind)).Attribute("label").ValueOrEmpty(); BaseFileEntry fsEntry = kind.Equals("folder") ? new BaseDirectoryEntry(title, 0, Convert.ToDateTime(updated).ToUniversalTime(), session.Service, session) : new BaseFileEntry(title, 0, Convert.ToDateTime(updated).ToUniversalTime(), session.Service, session); fsEntry.Id = resourceId; fsEntry.SetPropertyValue(GoogleDocsConstants.EtagProperty, etag); fsEntry.SetPropertyValue(GoogleDocsConstants.KindProperty, kind); if (kind.Equals("folder")) { var uploadUrl = entry.Elements(XName.Get("link", GoogleDocsConstants.AtomNamespace)).FirstOrDefault(x => x.Attribute("rel").ValueOrEmpty().Equals(GoogleDocsConstants.SchemeResCreateMedia)).AttributeOrNull("href").ValueOrEmpty(); fsEntry.SetPropertyValue(GoogleDocsConstants.ResCreateMediaProperty, uploadUrl); } else { var length = entry.Element(XName.Get("quotaBytesUsed", GoogleDocsConstants.GdNamespace)).ValueOrEmpty(); var downloadUrl = entry.Elements(XName.Get("content", GoogleDocsConstants.AtomNamespace)).FirstOrDefault().AttributeOrNull("src").ValueOrEmpty(); var editUrl = entry.Elements(XName.Get("link", GoogleDocsConstants.AtomNamespace)).FirstOrDefault(x => x.Attribute("rel").ValueOrEmpty().Equals(GoogleDocsConstants.SchemeResEditMedia)).AttributeOrNull("href").ValueOrEmpty(); fsEntry.Length = Convert.ToInt64(length); fsEntry.SetPropertyValue(GoogleDocsConstants.DownloadUrlProperty, downloadUrl); if (!String.IsNullOrEmpty(editUrl)) { fsEntry.SetPropertyValue(GoogleDocsConstants.ResEditMediaProperty, editUrl); } var ext = GoogleDocsResourceHelper.GetExtensionByKind(kind); if (!String.IsNullOrEmpty(ext)) { fsEntry.Name += '.' + ext; } } //var parents = entry.Elements(XName.Get("link", GoogleDocsConstants.AtomNamespace)) // .Where(x => x.AttributeOrNull("rel").ValueOrEmpty().Equals(GoogleDocsConstants.SchemeParent)) // .Select(x => // { // var parentUrl = x.ValueOrEmpty(); // var index = parentUrl.LastIndexOf('/'); // return parentUrl.Substring(index).Replace(':', '_').Replace("%3A", "_"); // }); //fsEntry.SetPropertyValue(GoogleDocsConstants.ParentsProperty, String.Join(",", parents.ToArray())); fsEntries.Add(fsEntry); } return(fsEntries); }