///<inheritdoc>
        public async Task UnlockAsync()
        {
            if (MsOfficeHelper.IsMsOfficeLocked(UserFileSystemPath)) // Required for PowerPoint. It does not block the for writing.
            {
                throw new ClientLockFailedException("The file is blocked for writing.");
            }

            ExternalDataManager customDataManager = Engine.CustomDataManager(UserFileSystemPath, Logger);
            LockManager         lockManager       = customDataManager.LockManager;

            // Set pending icon, so the user has a feedback as unlock operation may take some time.
            await customDataManager.SetLockPendingIconAsync(true);

            // Read lock-token from lock-info file.
            string lockToken = (await lockManager.GetLockInfoAsync()).LockToken;

            // Unlock the item in the remote storage here.

            // Delete lock-mode and lock-token info.
            lockManager.DeleteLock();

            // Remove lock icon and lock info in custom columns.
            await customDataManager.SetLockInfoAsync(null);

            Logger.LogMessage("Unlocked in the remote storage succesefully", UserFileSystemPath);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task <bool> FilterAsync(string userFileSystemPath, string userFileSystemNewPath = null)
        {
            // IsMsOfficeLocked() check is required for MS Office PowerPoint.
            // PowerPoint does not block the file for reading when the file is opened for editing.
            // As a result the file will be sent to the remote storage during each file save operation.

            if (userFileSystemNewPath == null)
            {
                // Executed during create, update, delete, open, close.
                return(MsOfficeHelper.AvoidMsOfficeSync(userFileSystemPath));
            }
            else
            {
                // Executed during rename/move operation.
                return
                    (MsOfficeHelper.IsRecycleBin(userFileSystemNewPath) || // When a hydrated file is deleted, it is moved to a Recycle Bin.
                     MsOfficeHelper.AvoidMsOfficeSync(userFileSystemNewPath));
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task WriteAsync(IFileMetadata fileMetadata, Stream content = null)
        {
            if (MsOfficeHelper.IsMsOfficeLocked(UserFileSystemPath)) // Required for PowerPoint. It does not block the for writing.
            {
                throw new ClientLockFailedException("The file is blocked for writing.");
            }

            Logger.LogMessage($"{nameof(IFile)}.{nameof(WriteAsync)}()", UserFileSystemPath);

            ExternalDataManager customDataManager = Engine.CustomDataManager(UserFileSystemPath);
            // Send the ETag to the server as part of the update to ensure the file in the remote storge is not modified since last read.
            string oldEtag = await customDataManager.ETagManager.GetETagAsync();

            // Send the lock-token to the server as part of the update.
            string lockToken = (await customDataManager.LockManager.GetLockInfoAsync())?.LockToken;

            FileInfo remoteStorageItem = new FileInfo(RemoteStoragePath);

            if (content != null)
            {
                // Upload remote storage file content.
                await using (FileStream remoteStorageStream = remoteStorageItem.Open(FileMode.Open, FileAccess.Write, FileShare.Delete))
                {
                    await content.CopyToAsync(remoteStorageStream);

                    remoteStorageStream.SetLength(content.Length);
                }
            }

            // Update remote storage file metadata.
            remoteStorageItem.Attributes        = fileMetadata.Attributes;
            remoteStorageItem.CreationTimeUtc   = fileMetadata.CreationTime.UtcDateTime;
            remoteStorageItem.LastWriteTimeUtc  = fileMetadata.LastWriteTime.UtcDateTime;
            remoteStorageItem.LastAccessTimeUtc = fileMetadata.LastAccessTime.UtcDateTime;
            remoteStorageItem.LastWriteTimeUtc  = fileMetadata.LastWriteTime.UtcDateTime;

            // Get the new ETag from server here as part of the update and save it on the client.
            string newEtag = "1234567890";
            await customDataManager.ETagManager.SetETagAsync(newEtag);

            await customDataManager.SetCustomColumnsAsync(new[] { new FileSystemItemPropertyData((int)CustomColumnIds.ETag, newEtag) });
        }
        /// <summary>
        /// Called when a file or folder is renamed in the user file system.
        /// </summary>
        private async void RenamedAsync(object sender, RenamedEventArgs e)
        {
            // If the item was previously filtered by EngineWindows.FilterAsync(),
            // for example temp MS Office file was renamed SGE4274H -> file.xlsx,
            // we need to convert the file to a pleaceholder and upload it to the remote storage.

            LogMessage("Renamed", e.OldFullPath, e.FullPath);

            string userFileSystemOldPath = e.OldFullPath;
            string userFileSystemNewPath = e.FullPath;

            try
            {
                if (System.IO.File.Exists(userFileSystemNewPath) &&
                    !MsOfficeHelper.AvoidMsOfficeSync(userFileSystemNewPath))
                {
                    if (!PlaceholderItem.IsPlaceholder(userFileSystemNewPath))
                    {
                        if (engine.CustomDataManager(userFileSystemNewPath).IsNew)
                        {
                            await engine.ClientNotifications(userFileSystemNewPath, this).CreateAsync();
                        }
                        else
                        {
                            LogMessage("Converting to placeholder", userFileSystemNewPath);
                            PlaceholderItem.ConvertToPlaceholder(userFileSystemNewPath, null, null, false);
                            await engine.ClientNotifications(userFileSystemNewPath, this).UpdateAsync();

                            await engine.CustomDataManager(userFileSystemNewPath).RefreshCustomColumnsAsync();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogError($"{e.ChangeType} failed", userFileSystemOldPath, userFileSystemNewPath, ex);
            }
        }