public void RefreshDirectoryContent(IStorageProviderSession session, BaseDirectoryEntry entry) { if (entry == null) return; var url = String.Format(GoogleDocsConstants.GoogleDocsContentsUrlFormat, entry.Id.ReplaceFirst("_", "%3a")); var parameters = new Dictionary<string, string> { { "max-results", "1000" } }; try { while (!String.IsNullOrEmpty(url)) { var request = CreateWebRequest(session, url, "GET", parameters); var response = (HttpWebResponse)request.GetResponse(); var rs = response.GetResponseStream(); var feedXml = new StreamReader(rs).ReadToEnd(); var childs = GoogleDocsXmlParser.ParseEntriesXml(session, feedXml); entry.AddChilds(childs); url = GoogleDocsXmlParser.ParseNext(feedXml); } } catch (WebException) { } }
private static ICloudFileSystemEntry ParseSingleEntry(IStorageProviderSession session, String json, JsonHelper parser) { if (json == null) return null; if (parser == null) parser = CreateParser(json); if (ContainsError(json, false, parser)) return null; BaseFileEntry entry; var type = parser.GetProperty("type"); if (!IsFolderType(type) && !IsFileType(type)) return null; var id = parser.GetProperty("id"); var name = parser.GetProperty("name"); var parentID = parser.GetProperty("parent_id"); var uploadLocation = parser.GetProperty("upload_location"); var updatedTime = Convert.ToDateTime(parser.GetProperty("updated_time")).ToUniversalTime(); if (IsFolderType(type)) { int count = parser.GetPropertyInt("count"); entry = new BaseDirectoryEntry(name, count, updatedTime, session.Service, session) {Id = id}; } else { var size = Convert.ToInt64(parser.GetProperty("size")); entry = new BaseFileEntry(name, size, updatedTime, session.Service, session) {Id = id}; } entry[SkyDriveConstants.UploadLocationKey] = uploadLocation; if (!String.IsNullOrEmpty(parentID)) { entry.ParentID = SkyDriveConstants.RootIDRegex.IsMatch(parentID) ? "/" : parentID; } else { entry.Name = "/"; entry.Id = "/"; } entry[SkyDriveConstants.InnerIDKey] = id; entry[SkyDriveConstants.TimestampKey] = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); return entry; }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="Name"></param> /// <param name="modifiedDate"></param> /// <param name="parent"></param> /// <returns></returns> public static ICloudDirectoryEntry CreateDirectoryEntry(IStorageProviderSession session, string Name, DateTime modifiedDate, ICloudDirectoryEntry parent) { // build up query url var newObj = new BaseDirectoryEntry(Name, 0, modifiedDate, session.Service, session); // case the parent if possible if (parent != null) { var objparent = parent as BaseDirectoryEntry; objparent.AddChild(newObj); } return newObj; }
public static BaseFileEntry UpdateObjectFromJsonString(String jsonMessage, BaseFileEntry objectToUpdate, IStorageProviderService service, IStorageProviderSession session) { // verify if we have a directory or a file var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonMessage)) throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName); var isDir = jc.GetBooleanProperty("is_dir"); // create the entry BaseFileEntry dbentry; Boolean bEntryOk; if (isDir) { if (objectToUpdate == null) dbentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate as BaseDirectoryEntry; bEntryOk = BuildDirectyEntry(dbentry as BaseDirectoryEntry, jc, service, session); } else { if (objectToUpdate == null) dbentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate; bEntryOk = BuildFileEntry(dbentry, jc); } // parse the childs and fill the entry as self if (!bEntryOk) throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService); // set the is deleted flag try { // try to read the is_deleted property dbentry.IsDeleted = jc.GetBooleanProperty("is_deleted"); } catch (Exception) { // the is_deleted proprty is missing (so it's not a deleted file or folder) dbentry.IsDeleted = false; } // return the child return dbentry; }
private static Boolean BuildDirectyEntry(BaseDirectoryEntry dirEntry, JsonHelper jh, IStorageProviderService service, IStorageProviderSession session) { // build the file entry part if (!BuildFileEntry(dirEntry, jh)) return false; // now take the content var content = jh.GetListProperty("contents"); if (content.Count == 0) return true; // remove all childs dirEntry.ClearChilds(); // add the childs foreach (var jsonContent in content) { // parse the item var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonContent)) continue; // check if we have a directory var isDir = jc.GetBooleanProperty("is_dir"); BaseFileEntry fentry; if (isDir) { fentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); } else { fentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); } // build the file attributes BuildFileEntry(fentry, jc); // establish parent child realtionship dirEntry.AddChild(fentry); } // set the length dirEntry.Length = dirEntry.Count; // go ahead return true; }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="Name"></param> /// <param name="parent"></param> /// <returns></returns> public override ICloudFileSystemEntry CreateResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent) { // get the credentials ICredentials creds = session.SessionToken as ICredentials; // build url String uriString = GetResourceUrl(session, parent, null); uriString = PathHelper.Combine(uriString, Name); Uri uri = new Uri(uriString); // build the DavService DavService svc = new DavService(); // create the webrequest int errorCode; WebException e; svc.PerformSimpleWebCall(uri.ToString(), WebRequestMethodsEx.Http.MkCol, creds.GetCredential(null, null), null, out errorCode, out e); if (errorCode != (int) HttpStatusCode.Created) return null; else { BaseDirectoryEntry newDir = new BaseDirectoryEntry(Name, 0, DateTime.Now, this, session); // init parent child relation if (parent != null) { BaseDirectoryEntry parentDir = parent as BaseDirectoryEntry; parentDir.AddChild(newDir); } return newDir; } }
private void RefreshChildsOfDirectory(IStorageProviderSession session, BaseDirectoryEntry dir) { // get the location String reslocation = GetResourceUrl(session, dir as ICloudFileSystemEntry, null); // ensure that we have a trailing slash reslocation = reslocation.TrimEnd('/'); reslocation = reslocation + "/"; // build the uri Uri resUri = new Uri(reslocation); // convert BaseDir to DirInfo DirectoryInfo dInfo = new DirectoryInfo(resUri.LocalPath); // clear childs dir.ClearChilds(); // get all childs foreach (FileSystemInfo fInfo in dInfo.GetFileSystemInfos()) { BaseFileEntry f = CreateEntryByFileSystemInfo(fInfo, session, dir); dir.AddChild(f); } }
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); }