Exemplo n.º 1
0
        public void AddChild(BaseFileEntry child)
        {
            // remove child if available
            RemoveChild(child);

            // add the new child
            _subDirectories.Add(child.Name, child);
            _subDirectoriesByIndex.Add(child);

            // set the parent
            child.Parent = this;
        }
        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 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;
        }
        public override ICloudFileSystemEntry RequestResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent)
        {
            // build path
            String path;

            if (Name.Equals("/"))
                path = session.ServiceConfiguration.ServiceLocator.LocalPath;
            else if (parent == null)
                path = Path.Combine(path = session.ServiceConfiguration.ServiceLocator.LocalPath, Name);
            else
                path = new Uri(GetResourceUrl(session, parent, Name)).LocalPath;

            // check if file exists
            if (File.Exists(path))
            {
                // create the fileinfo
                FileInfo fInfo = new FileInfo(path);

                BaseFileEntry bf = new BaseFileEntry(fInfo.Name, fInfo.Length, fInfo.LastWriteTimeUtc, this, session) {Parent = parent};

                // add to parent
                if (parent != null)
                    (parent as BaseDirectoryEntry).AddChild(bf);

                // go ahead
                return bf;
            }
                // check if directory exists
            else if (Directory.Exists(path))
            {
                // build directory info
                DirectoryInfo dInfo = new DirectoryInfo(path);

                // build bas dir
                BaseDirectoryEntry dir = CreateEntryByFileSystemInfo(dInfo, session, parent) as BaseDirectoryEntry;
                if (Name.Equals("/"))
                    dir.Name = "/";

                // add to parent
                if (parent != null)
                    (parent as BaseDirectoryEntry).AddChild(dir);

                // refresh the childs
                RefreshChildsOfDirectory(session, dir);

                // go ahead
                return dir;
            }
            else
                return null;
        }
        /// <summary>
        /// This method creates a filesystem entry 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="Name"></param>
        /// <param name="modified"></param>
        /// <param name="fileSize"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static ICloudFileSystemEntry CreateFileSystemEntry(IStorageProviderSession session, string Name, DateTime modified, long fileSize, ICloudDirectoryEntry parent)
        {
            // build up query url
            var newObj = new BaseFileEntry(Name, fileSize, modified, session.Service, session);

            // case the parent if possible
            if (parent != null)
            {
                var objparent = parent as BaseDirectoryEntry;
                objparent.AddChild(newObj);
            }

            return newObj;
        }
Exemplo n.º 6
0
        public void AddChild(BaseFileEntry child)
        {
            // remove child if available
            //RemoveChild(child);

            //don't need to remove child if contains; it's must be up to date due ProviderService's
            if (_subDirectories.ContainsKey(child.Id))
            {
                child.Parent = this;
                return;
            }

            // add the new child
            _subDirectories.Add(child.Id, child);
            _subDirectoriesByIndex.Add(child);

            // set the parent
            child.Parent = this;
        }
        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;
        }
        private static Boolean BuildFileEntry(BaseFileEntry fileEntry, JsonHelper jh)
        {
            /*
             *  "revision": 29251,
                "thumb_exists": false,
                "bytes": 37941660,
                "modified": "Tue, 01 Jun 2010 14:45:09 +0000",
                "path": "/Public/2010_06_01 15_53_48_336.nvl",
                "is_dir": false,
                "icon": "page_white",
                "mime_type": "application/octet-stream",
                "size": "36.2MB"
             * */

            // set the size
            fileEntry.Length = Convert.ToInt64(jh.GetProperty("bytes"));

            // set the modified time
            fileEntry.Modified = jh.GetDateTimeProperty("modified");

            // build the displayname
            var DropBoxPath = jh.GetProperty("path");
            var arr = DropBoxPath.Split('/');
            fileEntry.Name = arr.Length > 0 ? arr[arr.Length - 1] : DropBoxPath;

            if (DropBoxPath.Equals("/"))
            {
                fileEntry.Id = "/";
                fileEntry.ParentID = null;
            }
            else
            {
                fileEntry.Id = DropBoxPath.Trim('/');
                fileEntry.ParentID = DropBoxResourceIDHelpers.GetParentID(DropBoxPath);
            }
            

            // set the hash property if possible
            var hashValue = jh.GetProperty("hash");
            if (hashValue.Length > 0)
                fileEntry.SetPropertyValue("hash", hashValue);

            // set the path property            
            fileEntry.SetPropertyValue("path", DropBoxPath.Equals("/") ? "" : DropBoxPath);

            // set the revision value if possible
            var revValue = jh.GetProperty("rev");
            if (revValue.Length > 0)
            {
                fileEntry.SetPropertyValue("rev", revValue);
            }

            // go ahead
            return true;
        }
Exemplo n.º 9
0
 public void RemoveChild(BaseFileEntry child)
 {
     RemoveChildById(child.Id);
 }
        private bool MoveOrRenameOrCopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath, bool copy)
        {
            // build the path for resource
            var resourcePath = DropBoxResourceIDHelpers.GetResourcePath(orgEntry);

            // request the json object via oauth
            var parameters = new Dictionary<string, string>
                                 {
                                     {"from_path", resourcePath},
                                     {"root", GetRootToken(session)},
                                     {"to_path", toPath}
                                 };

            try
            {
                // move or rename the entry
                int code;
                var res = DropBoxRequestParser.RequestResourceByUrl(GetUrlString(copy ? DropBoxCopyItem : DropBoxMoveItem, session.ServiceConfiguration), parameters, this, session, out code);

                // update the entry
                DropBoxRequestParser.UpdateObjectFromJsonString(res, orgEntry, this, session);
            }
            catch (Exception)
            {
                return false;
            }

            orgEntry.Id = toPath;
            return true;
        }
 private bool CopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath)
 {
     return MoveOrRenameOrCopyItem(session, orgEntry, toPath, true);
 }
        public void AddChild(BaseFileEntry child)
        {
            // remove child if available
            //RemoveChild(child);

            //don't need to remove child if contains; it's must be up to date due ProviderService's
            if (_subDirectories.ContainsKey(child.Id))
            {
                child.Parent = this;
                return;   
            }

            // add the new child
            _subDirectories.Add(child.Id, child);
            _subDirectoriesByIndex.Add(child);

            // set the parent
            child.Parent = this;
        }
 public void RemoveChild(BaseFileEntry child)
 {
     RemoveChildById(child.Id);
 }
Exemplo n.º 14
0
 public void RemoveChild(BaseFileEntry child)
 {
     RemoveChildByName(child.Name);
 }