コード例 #1
0
        /// <inheritdoc/>
        public async Task OpenAsync(IOperationContext operationContext, IResultContext context)
        {
            Logger.LogMessage($"{nameof(IFile)}.{nameof(OpenAsync)}()", UserFileSystemPath);

            // Auto-lock the file.
            string userFileSystemFilePath = UserFileSystemPath;

            if (Engine.ChangesProcessingEnabled && FsPath.Exists(userFileSystemFilePath))
            {
                if (VirtualDrive.Settings.AutoLock &&
                    !FsPath.AvoidAutoLock(userFileSystemFilePath) &&
                    !await VirtualDrive.LockManager(userFileSystemFilePath, Logger).IsLockedAsync() &&
                    FsPath.IsWriteLocked(userFileSystemFilePath) &&
                    !new PlaceholderFile(userFileSystemFilePath).IsNew(VirtualDrive))
                {
                    RemoteStorageRawItem <IVirtualFile> remoteStorageRawItem = new RemoteStorageRawItem <IVirtualFile>(userFileSystemFilePath, VirtualDrive, Logger);
                    if (await remoteStorageRawItem.IsLockSupportedAsync())
                    {
                        try
                        {
                            await remoteStorageRawItem.LockAsync(LockMode.Auto);
                        }
                        catch (ClientLockFailedException ex)
                        {
                            // Lock file is blocked by a concurrent thread. This is a normal behaviour.
                            Logger.LogMessage(ex.Message, userFileSystemFilePath);
                        }
                    }
                }
            }
        }
コード例 #2
0
        //$<IFolder.CloseAsync
        /// <inheritdoc/>
        public async Task CloseAsync(IOperationContext operationContext, IResultContext context)
        {
            // Here, if the file in the user file system is modified (not in-sync), you will send the file content,
            // creation time, modification time and attributes to the remote storage.
            // We also send ETag, to make sure the changes on the server, if any, are not overwritten.

            Logger.LogMessage("IFile.CloseAsync()", UserFileSystemPath);

            string userFileSystemFilePath = UserFileSystemPath;

            // In case the file is moved it does not exist in user file system when CloseAsync() is called.
            if (Engine.ChangesProcessingEnabled &&
                FsPath.Exists(userFileSystemFilePath) &&
                !FsPath.AvoidSync(userFileSystemFilePath))
            {
                // In case the file is overwritten it is converted to a regular file prior to CloseAsync().
                // we need to convert it back into file/folder placeholder.
                if (!PlaceholderItem.IsPlaceholder(userFileSystemFilePath))
                {
                    PlaceholderItem.ConvertToPlaceholder(userFileSystemFilePath, false);
                    Logger.LogMessage("Converted to placeholder", userFileSystemFilePath);
                }

                try
                {
                    if (PlaceholderItem.GetItem(userFileSystemFilePath).IsNew())
                    {
                        // Create new file in the remote storage.
                        await RemoteStorageRawItem.CreateAsync(userFileSystemFilePath, Logger);
                    }
                    else
                    {
                        // Send content to remote storage. Unlock if auto-locked.
                        await new RemoteStorageRawItem(userFileSystemFilePath, Logger).UpdateAsync();
                    }
                }
                catch (IOException ex)
                {
                    // Either the file is already being synced in another thread or client or server file is blocked by concurrent process.
                    // This is a normal behaviour.
                    // The file must be synched by your synchronyzation service at a later time, when the file becomes available.
                    Logger.LogMessage("Failed to upload file. Possibly in use by an application or blocked for synchronization in another thread:", ex.Message);
                }
            }
        }