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); }
private static void CommitUploadOperation(object[] args) { var request = args[0] as WebRequest; var entry = args[1] as BaseFileEntry; if (request == null || entry == null) { return; } WebResponse response = request.GetResponse(); using (var rs = response.GetResponseStream()) { if (rs == null) { return; } String json = new StreamReader(rs).ReadToEnd(); String id = SkyDriveJsonParser.ParseEntryID(json); entry.Id = id; entry.Modified = DateTime.UtcNow; var parent = entry.Parent as BaseDirectoryEntry; if (parent != null) { parent.RemoveChildById(entry.Name); parent.AddChild(entry); } } }
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 ICloudFileSystemEntry CreateResource(IStorageProviderSession session, String name, ICloudDirectoryEntry parent) { if (name.Contains("/")) { throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName); } var uri = parent != null ? String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, parent.GetPropertyValue(SkyDriveConstants.InnerIDKey)) : SkyDriveConstants.RootAccessUrl; var data = String.Format("{{name: \"{0}\"}}", name); var json = SkyDriveRequestHelper.PerformRequest(session, uri, "POST", data, false); var entry = SkyDriveJsonParser.ParseSingleEntry(session, json); var parentBase = parent as BaseDirectoryEntry; if (parentBase != null && entry != null) { parentBase.AddChild(entry as BaseFileEntry); } return(entry); }
public void RefreshDirectoryContent(IStorageProviderSession session, ICloudDirectoryEntry directory) { String uri = String.Format(SkyDriveConstants.FilesAccessUrlFormat, directory.Id); uri = SignUri(session, uri); WebRequest request = WebRequest.Create(uri); WebResponse response = request.GetResponse(); using (var rs = response.GetResponseStream()) { if (rs == null) { throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotRetrieveDirectoryList); } String json = new StreamReader(rs).ReadToEnd(); var childs = SkyDriveJsonParser.ParseListOfEntries(session, json) .Select(x => x as BaseFileEntry).Where(x => x != null).ToArray(); if (childs.Length == 0 || !(directory is BaseDirectoryEntry)) { throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotRetrieveDirectoryList); } var directoryBase = directory as BaseDirectoryEntry; directoryBase.AddChilds(childs); } }
public override ICloudFileSystemEntry RequestResource(IStorageProviderSession session, String nameOrID, ICloudDirectoryEntry parent) { /* In this method name could be either requested resource name or it's ID. * In first case just refresh the parent and then search child with an appropriate. * In second case it does not matter if parent is null or not a parent because we use only resource ID. */ if (SkyDriveHelpers.IsResourceID(nameOrID) || nameOrID.Equals("/") && parent == null) //If request by ID or root folder requested { String uri = SkyDriveHelpers.IsResourceID(nameOrID) ? String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, nameOrID) : SkyDriveConstants.RootAccessUrl; uri = SignUri(session, uri); WebRequest request = WebRequest.Create(uri); WebResponse response = request.GetResponse(); using (var rs = response.GetResponseStream()) { if (rs != null) { String json = new StreamReader(rs).ReadToEnd(); ICloudFileSystemEntry entry = SkyDriveJsonParser.ParseSingleEntry(session, json); return(entry); } } } else { String uri = parent != null ? String.Format(SkyDriveConstants.FilesAccessUrlFormat, parent.Id) : SkyDriveConstants.RootAccessUrl + "/files"; uri = SignUri(session, uri); WebRequest request = WebRequest.Create(uri); WebResponse response = request.GetResponse(); using (var rs = response.GetResponseStream()) { if (rs != null) { String json = new StreamReader(rs).ReadToEnd(); var entry = SkyDriveJsonParser.ParseListOfEntries(session, json).FirstOrDefault(x => x.Name.Equals(nameOrID)); if (entry != null && parent != null && parent is BaseDirectoryEntry) { (parent as BaseDirectoryEntry).AddChild(entry as BaseFileEntry); } return(entry); } } } return(null); }
private static void CommitUploadOperation(object[] args) { var tempStream = (Stream)args[0]; var contentLength = (long)args[1]; var session = (IStorageProviderSession)args[2]; var file = (BaseFileEntry)args[3]; var request = (HttpWebRequest)WebRequest.Create(GetSignedUploadUrl(session, file)); request.Method = "PUT"; request.ContentLength = contentLength; request.Timeout = Timeout.Infinite; tempStream.Flush(); tempStream.Seek(0, SeekOrigin.Begin); using (var requestSteam = request.GetRequestStream()) { tempStream.CopyTo(requestSteam); } tempStream.Close(); using (var response = request.GetResponse()) using (var rs = response.GetResponseStream()) { if (rs == null) { return; } string json; using (var streamReader = new StreamReader(rs)) { json = streamReader.ReadToEnd(); } var id = SkyDriveJsonParser.ParseEntryID(json); file.Id = id; file[SkyDriveConstants.InnerIDKey] = id; file.Modified = DateTime.UtcNow; var parent = file.Parent as BaseDirectoryEntry; if (parent != null) { parent.RemoveChildById(file.Name); parent.AddChild(file); } } }
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); }
public override bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry) { String uri = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.Id); uri = SignUri(session, uri); String json = PerformRequest(session, uri, "DELETE", null); if (json != null && !SkyDriveJsonParser.ContainsError(json, false)) { var parent = entry.Parent as BaseDirectoryEntry; if (parent != null) { parent.RemoveChildById(entry.Id); } return(true); } return(false); }
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 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.Id); String data = String.Format("{{name: \"{0}\"}}", newName); String json = PerformRequest(session, uri, "PUT", data); if (json != null && !SkyDriveJsonParser.ContainsError(json, false)) { var entryBase = entry as BaseFileEntry; if (entryBase != null) { entryBase.Name = newName; } return(true); } return(false); }
private static ICloudFileSystemEntry RequestResourseByUrl(IStorageProviderSession session, String url) { var json = SkyDriveRequestHelper.PerformRequest(session, url); return(SkyDriveJsonParser.ParseSingleEntry(session, json)); }
private static IEnumerable <ICloudFileSystemEntry> RequestContentByUrl(IStorageProviderSession session, String url) { var json = SkyDriveRequestHelper.PerformRequest(session, url); return(SkyDriveJsonParser.ParseListOfEntries(session, json) ?? new List <ICloudFileSystemEntry>()); }