示例#1
0
        /// <summary>
        /// Creates or updates folder in the remote storage.
        /// </summary>
        /// <param name="remoteStoragePath">Path of the folder to be created or updated in the remote storage.</param>
        /// <param name="newInfo">New information about the folder, such as modification date, attributes, custom data, etc.</param>
        /// <param name="mode">Specifies if a new folder should be created or existing folder should be updated.</param>
        /// <param name="lockInfo">Information about the lock. Caller passes null if the item is not locked.</param>
        /// <returns>New ETag returned from the remote storage.</returns>
        protected async Task <string> CreateOrUpdateFolderAsync(string remoteStoragePath, IFolderBasicInfo newInfo, FileMode mode, ServerLockInfo lockInfo = null)
        {
            // Get ETag and lock-token here and send it to the remote storage with the new item content/info if needed.
            if (mode == FileMode.Open)
            {
                // Get ETag.
                string eTag = await ETag.GetETagAsync(UserFileSystemPath);

                // Get lock-token.
                string lockToken = lockInfo?.LockToken;
            }

            DirectoryInfo remoteStorageItem = new DirectoryInfo(remoteStoragePath);

            try
            {
                Program.RemoteStorageMonitorInstance.Enabled = false; // Disable RemoteStorageMonitor to avoid circular calls.

                remoteStorageItem.Create();

                string userFileSystemPath = Mapping.ReverseMapPath(remoteStoragePath);
                if (mode == FileMode.Open)
                {
                    // Verify that the item in the remote storage is not modified since it was downloaded to the user file system.
                    // In your real-life application you will send the ETag to the server as part of the update request.
                    FileSystemItemBasicInfo itemInfo = Mapping.GetUserFileSysteItemBasicInfo(remoteStorageItem);
                    if (!(await ETag.ETagEqualsAsync(userFileSystemPath, itemInfo)))
                    {
                        throw new ConflictException(Modified.Server, "Item is modified in the remote storage, ETags not equal.");
                    }
                }

                // Update ETag/LastWriteTime in user file system, so the synchronyzation or remote storage monitor would not start the new update.
                // This is only required to avoid circular updates because of the simplicity of this sample.
                // In your real-life application you will receive a new ETag from server in the update response.
                string eTag = newInfo.LastWriteTime.DateTime.ToBinary().ToString();
                await ETag.SetETagAsync(userFileSystemPath, eTag);

                remoteStorageItem.Attributes        = newInfo.Attributes;
                remoteStorageItem.CreationTimeUtc   = newInfo.CreationTime.DateTime;
                remoteStorageItem.LastWriteTimeUtc  = newInfo.LastWriteTime.DateTime;
                remoteStorageItem.LastAccessTimeUtc = newInfo.LastAccessTime.DateTime;
                remoteStorageItem.LastWriteTimeUtc  = newInfo.LastWriteTime.DateTime;

                return(eTag);
            }
            finally
            {
                Program.RemoteStorageMonitorInstance.Enabled = true;
            }
        }
        /// <summary>
        /// Returns true if the item was created and must be synched to remote storage.
        /// </summary>
        /// <returns>
        /// True if the item was created in the user file system and does not exists
        /// in the remote storage. False otherwise.
        /// </returns>
        public static bool IsNew(this PlaceholderItem placeholder)
        {
            // ETag absence signals that the item is new.
            // However, ETag file may not exists during move operation,
            // additionally checking OriginalPath presence.
            // Can not rely on OriginalPath only,
            // because MS Office files are being deleted and re-created during transactional save.

            string originalPath = placeholder.GetOriginalPath();

            bool eTagFileExists = File.Exists(ETag.GetETagFilePath(placeholder.Path));

            return(!eTagFileExists && string.IsNullOrEmpty(originalPath));
        }
示例#3
0
        /// <summary>
        /// Returns true if the remote storage ETag and user file system ETags are equal. False - otherwise.
        /// </summary>
        /// <param name="userFileSystemPath">User file system item.</param>
        /// <param name="remoteStorageItem">Remote storage item info.</param>
        /// <remarks>
        /// ETag is updated on the server during every document update and is sent to client with a file.
        /// During client->server update it is sent back to the remote storage together with a modified content.
        /// This ensures the changes on the server are not overwritten if the document on the server is modified.
        /// </remarks>
        internal static async Task <bool> ETagEqualsAsync(string userFileSystemPath, FileSystemItemBasicInfo remoteStorageItem)
        {
            string remoteStorageETag = remoteStorageItem.ETag;

            // Intstead of the real ETag we store remote storage LastWriteTime when
            // creating and updating files/folders.
            string userFileSystemETag = await ETag.GetETagAsync(userFileSystemPath);

            if (string.IsNullOrEmpty(userFileSystemETag))
            {
                // No ETag associated with the file. This is a new file created in user file system.
                return(false);
            }

            return(remoteStorageETag == userFileSystemETag);
        }
        /// <inheritdoc/>
        public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
        {
            // This method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call one of the following:
            // - resultContext.ReturnChildren() method.
            // - resultContext.ReportProgress() method.

            Logger.LogMessage($"IFolder.GetChildrenAsync({pattern})", UserFileSystemPath);

            IEnumerable <FileSystemItemBasicInfo> children = await new UserFolder(UserFileSystemPath).EnumerateChildrenAsync(pattern);

            // Filtering existing files/folders. This is only required to avoid extra errors in the log.
            List <IFileSystemItemBasicInfo> newChildren = new List <IFileSystemItemBasicInfo>();

            foreach (IFileSystemItemBasicInfo child in children)
            {
                string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);
                if (!FsPath.Exists(userFileSystemItemPath))
                {
                    Logger.LogMessage("Creating", child.Name);
                    newChildren.Add(child);
                }
            }

            // To signal that the children enumeration is completed
            // always call ReturnChildren(), even if the folder is empty.
            resultContext.ReturnChildren(newChildren.ToArray(), newChildren.Count());


            // Save ETags and set "locked by another user" icon.
            foreach (FileSystemItemBasicInfo child in children)
            {
                string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);

                // Create ETags.
                // ETags must correspond with a server file/folder, NOT with a client placeholder.
                // It should NOT be moved/deleted/updated when a placeholder in the user file system is moved/deleted/updated.
                // It should be moved/deleted when a file/folder in the remote storage is moved/deleted.
                ETag.SetETagAsync(userFileSystemItemPath, child.ETag);

                // Set the lock icon and read-only attribute, to indicate that the item is locked by another user.
                new UserFileSystemRawItem(userFileSystemItemPath).SetLockedByAnotherUserAsync(child.LockedByAnotherUser);
            }
        }
        /// <summary>
        /// Creates or updates file in the remote storage.
        /// </summary>
        /// <param name="remoteStoragePath">Path of the file to be created or updated in the remote storage.</param>
        /// <param name="newInfo">New information about the file, such as modification date, attributes, custom data, etc.</param>
        /// <param name="mode">Specifies if a new file should be created or existing file should be updated.</param>
        /// <param name="newContentStream">New file content or null if the file content is not modified.</param>
        /// <returns>New ETag returned from the remote storage.</returns>
        protected async Task <string> CreateOrUpdateFileAsync(string remoteStoragePath, IFileBasicInfo newInfo, FileMode mode, Stream newContentStream = null)
        {
            // Get ETag and lock-token here and send it to the remote storage with the new item content/info if needed.
            if (mode == FileMode.Open)
            {
                // Get ETag.
                string eTag = await ETag.GetETagAsync(UserFileSystemPath);

                // Get lock-token.
                string lockToken = Lock?.LockToken;
            }

            FileInfo remoteStorageItem = new FileInfo(remoteStoragePath);

            try
            {
                Program.RemoteStorageMonitorInstance.Enabled = false; // Disable RemoteStorageMonitor to avoid circular calls.

                // If another thread is trying to sync the same file, this call will fail in other threads.
                // In your implementation you must lock your remote storage file, or block it for reading and writing by other means.
                await using (FileStream remoteStorageStream = remoteStorageItem.Open(mode, FileAccess.Write, FileShare.None))
                {
                    string userFileSystemPath = Mapping.ReverseMapPath(remoteStoragePath);
                    if (mode == FileMode.Open)
                    {
                        // Verify that the item in the remote storage is not modified since it was downloaded to the user file system.
                        // In your real-life application you will send the ETag to the server as part of the update request.
                        FileSystemItemBasicInfo itemInfo = Mapping.GetUserFileSysteItemBasicInfo(remoteStorageItem);
                        if (!(await ETag.ETagEqualsAsync(userFileSystemPath, itemInfo)))
                        {
                            throw new ConflictException(Modified.Server, "Item is modified in the remote storage, ETags not equal.");
                        }
                    }

                    // Update ETag/LastWriteTime in user file system, so the synchronyzation or remote storage monitor would not start the new update.
                    // This is only required to avoid circular updates because of the simplicity of this sample.
                    // In your real-life application you will receive a new ETag from the server in the update response
                    // and return it from this method.
                    string eTag = newInfo.LastWriteTime.ToBinary().ToString();
                    await ETag.SetETagAsync(userFileSystemPath, eTag);

                    // Update remote storage file content.
                    if (newContentStream != null)
                    {
                        await newContentStream.CopyToAsync(remoteStorageStream);

                        remoteStorageStream.SetLength(newContentStream.Length);
                    }

                    // Update remote storage file basic info.
                    WindowsFileSystemItem.SetFileInformation(
                        remoteStorageStream.SafeFileHandle,
                        newInfo.Attributes,
                        newInfo.CreationTime,
                        newInfo.LastWriteTime,
                        newInfo.LastAccessTime,
                        newInfo.LastWriteTime);

                    return(eTag);
                }
            }
            finally
            {
                Program.RemoteStorageMonitorInstance.Enabled = true;
            }
        }