Ejemplo n.º 1
0
        /// <summary>
        /// Creates or updates this item in the remote storage.
        /// </summary>
        /// <param name="remoteStorageUri">Uri of the file to be created or updated in the remote storage.</param>
        /// <param name="newInfo">New information about the file or folder, 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="content">New file content or null if the file content is not modified.</param>
        /// <param name="eTagOld">The ETag to be sent to the remote storage as part of the update request to make sure the content is not overwritten.</param>
        /// <param name="lockInfo">Information about the lock. Caller passes null if the item is not locked.</param>
        /// <returns>The new ETag returned from the remote storage.</returns>
        protected static async Task <string> CreateOrUpdateFileAsync(
            Uri remoteStorageUri, IFileSystemItemMetadata newInfo, FileMode mode, Stream content = null, string eTagOld = null, ServerLockInfo lockInfo = null)
        {
            string eTagNew = null;

            if (content != null || mode == FileMode.CreateNew)
            {
                long contentLength = content != null ? content.Length : 0;

                IWebRequestAsync request = await Program.DavClient.GetFileWriteRequestAsync(
                    remoteStorageUri, null, contentLength, 0, -1, lockInfo?.LockToken, eTagOld);

                // Update remote storage file content.
                using (Stream davContentStream = await request.GetRequestStreamAsync())
                {
                    if (content != null)
                    {
                        await content.CopyToAsync(davContentStream);
                    }

                    // Get the new ETag returned by the server (if any).
                    // We return the new ETag to the Engine to be stored with the file unlil the next update.
                    IWebResponseAsync response = await request.GetResponseAsync();

                    eTagNew = response.Headers["ETag"];
                    response.Close();
                }
            }
            return(eTagNew);
        }
Ejemplo n.º 2
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;


            if (content != null)
            {
                long contentLength = content != null ? content.Length : 0;

                IWebRequestAsync request = await Program.DavClient.GetFileWriteRequestAsync(
                    new Uri(RemoteStoragePath), null, contentLength, 0, -1, lockToken, oldEtag);

                // Update remote storage file content.
                using (Stream davContentStream = await request.GetRequestStreamAsync())
                {
                    if (content != null)
                    {
                        await content.CopyToAsync(davContentStream);
                    }

                    // Get the new ETag returned by the server (if any).
                    IWebResponseAsync response = await request.GetResponseAsync();

                    string eTagNew = response.Headers["ETag"];
                    response.Close();

                    // Store ETag unlil the next update.
                    // This will also mark the item as not new, which is required for correct MS Office saving opertions.
                    await customDataManager.ETagManager.SetETagAsync(eTagNew);

                    // Update ETag in custom column displayed in file manager.
                    await customDataManager.SetCustomColumnsAsync(new[] { new FileSystemItemPropertyData((int)CustomColumnIds.ETag, eTagNew) });
                }
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task <byte[]> CreateFileAsync(IFileMetadata fileMetadata, Stream content = null)
        {
            string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, fileMetadata.Name);

            Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", userFileSystemNewItemPath);

            Uri newFileUri = new Uri(new Uri(RemoteStoragePath), fileMetadata.Name);

            long contentLength = content != null ? content.Length : 0;

            IWebRequestAsync request = await Program.DavClient.GetFileWriteRequestAsync(newFileUri, null, contentLength);

            // Update remote storage file content.
            using (Stream davContentStream = await request.GetRequestStreamAsync())
            {
                if (content != null)
                {
                    await content.CopyToAsync(davContentStream);
                }

                // Get the new ETag returned by the server (if any).
                IWebResponseAsync response = await request.GetResponseAsync();

                string eTagNew = response.Headers["ETag"];
                response.Close();

                ExternalDataManager customDataManager = Engine.CustomDataManager(userFileSystemNewItemPath);

                // Store ETag unlil the next update.
                // This will also mark the item as not new, which is required for correct MS Office saving opertions.
                await customDataManager.ETagManager.SetETagAsync(eTagNew);

                customDataManager.IsNew = false; // Mark file as not new just in case the server did not return the ETag.

                // Update ETag in custom column displayed in file manager.
                await customDataManager.SetCustomColumnsAsync(new[] { new FileSystemItemPropertyData((int)CustomColumnIds.ETag, eTagNew) });
            }

            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates or updates file in the remote storage.
        /// </summary>
        /// <param name="remoteStorageUri">Uri 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="content">New file content or null if the file content is not modified.</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> CreateOrUpdateFileAsync(Uri remoteStorageUri, IFileBasicInfo newInfo, FileMode mode, Stream content = null, ServerLockInfo lockInfo = null)
        {
            string eTag      = null;
            string lockToken = null;

            // Get ETag and lock-token here and send it to the remote storage with the new item content/info.
            if (mode == FileMode.Open)
            {
                // Get ETag.
                eTag = await ETag.GetETagAsync(UserFileSystemPath);

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

            if (content != null || mode == FileMode.CreateNew)
            {
                long contentLength = content != null ? content.Length : 0;

                IWebRequestAsync request = await Program.DavClient.GetFileWriteRequestAsync(remoteStorageUri, null, contentLength, 0, -1, lockToken, eTag);

                // Update remote storage file content.
                using (Stream davContentStream = await request.GetRequestStreamAsync())
                {
                    if (content != null)
                    {
                        await content.CopyToAsync(davContentStream);
                    }

                    // Get ETag returned by the server, if any.
                    IWebResponseAsync response = await request.GetResponseAsync();

                    eTag = response.Headers["ETag"];
                    response.Close();
                }
            }
            return(eTag);
        }