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);
        }
        public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
        {
            //Request resource by ID and then update properties from requested
            var current = RequestResource(session, resource.Id, null);

            SkyDriveHelpers.CopyProperties(current, resource);

            if (resource is ICloudDirectoryEntry)
            {
                RefreshDirectoryContent(session, resource as ICloudDirectoryEntry);
            }
        }
Beispiel #3
0
        public override string GetResourceUrl(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, string additionalPath)
        {
            var id = SkyDriveHelpers.GetResourceID(additionalPath);

            if (!String.IsNullOrEmpty(id) || additionalPath != null && additionalPath.Equals("/"))
            {
                return("/" + id);
            }
            if (String.IsNullOrEmpty(additionalPath) && fileSystemEntry != null)
            {
                return("/" + (!fileSystemEntry.Id.Equals("/") ? fileSystemEntry.Id : ""));
            }
            return(base.GetResourceUrl(session, fileSystemEntry, additionalPath));
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        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.HasResourceID(nameOrID) || nameOrID.Equals("/") && parent == null)
            //If request by ID or root folder requested
            {
                var id  = SkyDriveHelpers.GetResourceID(nameOrID);
                var uri = id != String.Empty
                                 ? String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, id)
                                 : SkyDriveConstants.RootAccessUrl;

                if (SkyDriveHelpers.IsFolderID(id))
                {
                    var contents             = new List <ICloudFileSystemEntry>();
                    BaseDirectoryEntry entry = null;

                    /*var completeContent = new AutoResetEvent(false);
                     * ThreadPool.QueueUserWorkItem(state =>
                     *  {
                     *      contents.AddRange(RequestContentByUrl(session, uri + "/files"));
                     *      ((AutoResetEvent) state).Set();
                     *  }, completeContent);
                     *
                     * var completeEntry = new AutoResetEvent(false);
                     * ThreadPool.QueueUserWorkItem(state =>
                     *  {
                     *      entry = RequestResourseByUrl(session, uri) as BaseDirectoryEntry;
                     *      ((AutoResetEvent) state).Set();
                     *  }, completeEntry);
                     *
                     * WaitHandle.WaitAll(new WaitHandle[] {completeContent, completeEntry});*/

                    entry = RequestResourseByUrl(session, uri) as BaseDirectoryEntry;
                    contents.AddRange(RequestContentByUrl(session, uri + "/files"));

                    if (entry != null && contents.Any())
                    {
                        entry.AddChilds(contents.Cast <BaseFileEntry>());
                    }

                    return(entry);
                }

                return(RequestResourseByUrl(session, uri));
            }
            else
            {
                String uri;
                if (SkyDriveHelpers.HasParentID(nameOrID))
                {
                    uri = String.Format(SkyDriveConstants.FilesAccessUrlFormat, SkyDriveHelpers.GetParentID(nameOrID));
                }
                else if (parent != null)
                {
                    uri = String.Format(SkyDriveConstants.FilesAccessUrlFormat, parent.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                }
                else
                {
                    uri = SkyDriveConstants.RootAccessUrl + "/files";
                }

                nameOrID = Path.GetFileName(nameOrID);
                var entry = RequestContentByUrl(session, uri).FirstOrDefault(x => x.Name.Equals(nameOrID));
                return(entry);
            }
        }