/// <summary>
        /// Gets a user file system item info from the remote storage data.
        /// </summary>
        /// <param name="remoteStorageItem">Remote storage item info.</param>
        /// <returns>User file system item info.</returns>
        public static FileSystemItemMetadataExt GetUserFileSysteItemMetadata(FileSystemInfo remoteStorageItem)
        {
            FileSystemItemMetadataExt userFileSystemItem;

            if (remoteStorageItem is FileInfo)
            {
                userFileSystemItem = new FileMetadataExt();
            }
            else
            {
                userFileSystemItem = new FolderMetadataExt();
            }

            userFileSystemItem.Name           = remoteStorageItem.Name;
            userFileSystemItem.Attributes     = remoteStorageItem.Attributes;
            userFileSystemItem.CreationTime   = remoteStorageItem.CreationTime;
            userFileSystemItem.LastWriteTime  = remoteStorageItem.LastWriteTime;
            userFileSystemItem.LastAccessTime = remoteStorageItem.LastAccessTime;
            userFileSystemItem.ChangeTime     = remoteStorageItem.LastWriteTime;

            // You will send the ETag to
            // the server inside If-Match header togeter with updated content from client.
            // This will make sure the changes on the server is not overwritten.
            //
            // In this sample, for the sake of simplicity, we use file last write time instead of ETag.
            userFileSystemItem.ETag = userFileSystemItem.LastWriteTime.ToUniversalTime().ToString("o");

            // If the item is locked by another user, set the LockedByAnotherUser to true.
            // Here we just use the read-only attribute from remote storage item for demo purposes.
            userFileSystemItem.LockedByAnotherUser = (remoteStorageItem.Attributes & System.IO.FileAttributes.ReadOnly) != 0;

            if (remoteStorageItem is FileInfo)
            {
                ((FileMetadataExt)userFileSystemItem).Length = ((FileInfo)remoteStorageItem).Length;
            }
            ;

            // Set custom columns to be displayed in file manager.
            // We create property definitions when registering the sync root with corresponding IDs.
            List <FileSystemItemPropertyData> customProps = new List <FileSystemItemPropertyData>();

            if (userFileSystemItem.LockedByAnotherUser)
            {
                customProps.AddRange(
                    new ServerLockInfo()
                {
                    LockToken             = "token",
                    Owner                 = "User Name",
                    Exclusive             = true,
                    LockExpirationDateUtc = DateTimeOffset.Now.AddMinutes(30)
                }.GetLockProperties(Path.Combine(Program.Settings.IconsFolderPath, "LockedByAnotherUser.ico"))
                    );
            }
            userFileSystemItem.CustomProperties = customProps;

            return(userFileSystemItem);
        }
Example #2
0
        /// <summary>
        /// Gets file system item info from the user file system item.
        /// Removes Pinned, Unpinned and Offline flags from attributes.
        /// </summary>
        /// <param name="userFileSystemItem">User file system item info.</param>
        /// <returns>Object implementing <see cref="IFileSystemItemMetadata"/> interface.</returns>
        private static IFileSystemItemMetadata GetMetadata(FileSystemInfo userFileSystemItem)
        {
            FileSystemItemMetadataExt itemInfo;

            if (userFileSystemItem is FileInfo)
            {
                itemInfo = new FileMetadataExt();
            }
            else
            {
                itemInfo = new FolderMetadataExt();
            }

            // Remove Pinned, unpinned and offline flags,
            // so they are not sent to the remote storage by mistake.
            FileAttributes flags = userFileSystemItem.Attributes
                                   & (FileAttributes) ~FileAttributesExt.Pinned
                                   & (FileAttributes) ~FileAttributesExt.Unpinned
                                   & (FileAttributes) ~FileAttributesExt.Offline;

            itemInfo.Name           = userFileSystemItem.Name;
            itemInfo.Attributes     = flags;
            itemInfo.CreationTime   = userFileSystemItem.CreationTime;
            itemInfo.LastWriteTime  = userFileSystemItem.LastWriteTime;
            itemInfo.LastAccessTime = userFileSystemItem.LastAccessTime;
            itemInfo.ChangeTime     = userFileSystemItem.LastWriteTime;

            itemInfo.CustomData = PlaceholderItem.GetItem(userFileSystemItem.FullName).GetCustomData();

            if (userFileSystemItem is FileInfo)
            {
                ((FileMetadataExt)itemInfo).Length = ((FileInfo)userFileSystemItem).Length;
            }
            ;

            return(itemInfo);
        }
Example #3
0
        /// <summary>
        /// Gets a user file system item info from the remote storage data.
        /// </summary>
        /// <param name="remoteStorageItem">Remote storage item info.</param>
        /// <returns>User file system item info.</returns>
        public static FileSystemItemMetadataExt GetUserFileSystemItemMetadata(IHierarchyItemAsync remoteStorageItem)
        {
            FileSystemItemMetadataExt userFileSystemItem;

            if (remoteStorageItem is IFileAsync)
            {
                userFileSystemItem = new FileMetadataExt();
            }
            else
            {
                userFileSystemItem = new FolderMetadataExt();
            }

            userFileSystemItem.Name           = remoteStorageItem.DisplayName;
            userFileSystemItem.Attributes     = FileAttributes.Normal;
            userFileSystemItem.CreationTime   = remoteStorageItem.CreationDate;
            userFileSystemItem.LastWriteTime  = remoteStorageItem.LastModified;
            userFileSystemItem.LastAccessTime = remoteStorageItem.LastModified;
            userFileSystemItem.ChangeTime     = remoteStorageItem.LastModified;

            // If the item is locked by another user, set the LockedByAnotherUser to true.
            // This will set read-only arrtibute on files.
            // Note that read-only attribute is a convenience feature, it does not protect files from modification.
            userFileSystemItem.LockedByAnotherUser = remoteStorageItem.ActiveLocks.Length > 0;

            if (remoteStorageItem is IFileAsync)
            {
                // We send the ETag to
                // the server inside If-Match header togeter with updated content from client.
                // This will make sure the changes on the server is not overwritten.
                ((FileMetadataExt)userFileSystemItem).ETag = ((IFileAsync)remoteStorageItem).Etag;

                ((FileMetadataExt)userFileSystemItem).Length = ((IFileAsync)remoteStorageItem).ContentLength;
            }
            ;

            // Set custom columns to be displayed in file manager.
            // We create property definitions when registering the sync root with corresponding IDs.
            List <FileSystemItemPropertyData> customProps = new List <FileSystemItemPropertyData>();
            LockInfo lockInfo = remoteStorageItem.ActiveLocks.FirstOrDefault();

            if (lockInfo != null)
            {
                customProps.AddRange(
                    new ServerLockInfo()
                {
                    LockToken             = lockInfo.LockToken.LockToken,
                    Owner                 = lockInfo.Owner,
                    Exclusive             = lockInfo.LockScope == LockScope.Exclusive,
                    LockExpirationDateUtc = DateTimeOffset.Now.Add(lockInfo.TimeOut)
                }.GetLockProperties(Path.Combine(Program.Settings.IconsFolderPath, "LockedByAnotherUser.ico"))
                    );
            }
            if (remoteStorageItem is IFileAsync)
            {
                customProps.Add(new FileSystemItemPropertyData((int)CustomColumnIds.ETag, ((IFileAsync)remoteStorageItem).Etag));
            }
            ;
            userFileSystemItem.CustomProperties = customProps;

            return(userFileSystemItem);
        }