Ejemplo n.º 1
0
        /// <summary>
        /// Accesses a file's contents as a stream for reading.
        /// </summary>
        /// <param name="objectFileOperations">The instance of <see cref="VaultObjectFileOperations"/> to use.</param>
        /// <param name="objectFile">The file to open for reading.</param>
        /// <param name="vault">The vault the file comes from.</param>
        /// <param name="fileFormat">The format of the file to read as.</param>
        /// <returns>The file opened as a <see cref="Stream"/>.</returns>
        public static Stream OpenFileForReading
        (
            this VaultObjectFileOperations objectFileOperations,
            ObjectFile objectFile,
            Vault vault,
            MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
        )
        {
            // Sanity.
            if (null == objectFileOperations)
            {
                throw new ArgumentNullException(nameof(objectFileOperations));
            }
            if (null == objectFile)
            {
                throw new ArgumentNullException(nameof(objectFile));
            }
            if (null == vault)
            {
                throw new ArgumentNullException(nameof(vault));
            }

            // Return a file stream of the object.
            return(new FileDownloadStream(objectFile, vault, fileFormat));
        }
        /// <summary>
        /// Downloads the file to disk.
        /// </summary>
        /// <param name="objectFile">The file to download.</param>
        /// <param name="vault">The vault to download from.</param>
        /// <param name="fileDownloadLocation">The location on disk to download to.</param>
        /// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
        /// <param name="fileFormat">The format of file to request from server.</param>
        /// <returns>A <see cref="TemporaryFileDownload"/> representing the completed file download.</returns>
        public static TemporaryFileDownload Download
        (
            this ObjectFile objectFile,
            Vault vault,
            FileDownloadLocation fileDownloadLocation,
            int blockSize           = FileTransfers.DefaultBlockSize,
            MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
        )
        {
            // Sanity.
            if (null == objectFile)
            {
                throw new ArgumentNullException(nameof(objectFile));
            }
            if (null == vault)
            {
                throw new ArgumentNullException(nameof(vault));
            }
            if (null == fileDownloadLocation)
            {
                throw new ArgumentNullException(nameof(fileDownloadLocation));
            }

            // Download the file.
            return(fileDownloadLocation
                   .DownloadFile
                   (
                       objectFile,
                       vault,
                       blockSize,
                       fileFormat
                   ));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds all files on the given <paramref name="objVerEx"/> to the <paramref name="emailMessage"/>.
        /// </summary>
        /// <param name="emailMessage">The email message to add the files to.</param>
        /// <param name="objVerEx">The object version that has the files.</param>
        /// <param name="fileFormat">The format for the attached files.</param>
        public static void AddAllFiles
        (
            this IEmailMessage emailMessage,
            ObjVerEx objVerEx,
            MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
        )
        {
            // Sanity.
            if (null == emailMessage)
            {
                throw new ArgumentNullException(nameof(emailMessage));
            }
            if (null == objVerEx)
            {
                throw new ArgumentNullException(nameof(objVerEx));
            }
            if (null == objVerEx.Vault)
            {
                throw new ArgumentException("The objVerEx's vault reference is null.", nameof(objVerEx));
            }
            if (null == objVerEx.Vault)
            {
                throw new ArgumentException("The objVerEx's info (ObjectVersion) reference is null.", nameof(objVerEx));
            }

            // Use the base implementation.
            emailMessage.AddAllFiles(objVerEx.Info, objVerEx.Vault, fileFormat);
        }
        public void OpenDownloadSessionPopulatesDownloadSession
        (
            int fileId,
            int fileVersion,
            MFFileFormat fileFormat
        )
        {
            // Set up a file to download.
            var objectFileMock = new Mock <ObjectFile>();

            objectFileMock.SetupGet(m => m.ID).Returns(fileId);
            objectFileMock.SetupGet(m => m.Version).Returns(fileVersion);

            // Set up the vault object file operations mock.
            var vaultObjectFileOperationsMock = new Mock <VaultObjectFileOperations>();

            // When DownloadFileInBlocks_BeginEx is called, return a dummy session.
            vaultObjectFileOperationsMock
            .Setup(m => m.DownloadFileInBlocks_BeginEx
                   (
                       Moq.It.IsAny <int>(),
                       Moq.It.IsAny <int>(),
                       Moq.It.IsAny <MFFileFormat>()
                   ))
            .Returns((int receivedFileId, int receivedFileVersion, MFFileFormat receivedFileFormat) =>
            {
                // Ensure we got the right data.
                Assert.AreEqual(fileId, receivedFileId);
                Assert.AreEqual(fileVersion, receivedFileVersion);
                Assert.AreEqual(fileFormat, receivedFileFormat);

                // Mock a download session to return.
                var downloadSessionMock = new Mock <FileDownloadSession>();
                downloadSessionMock.SetupGet(m => m.DownloadID).Returns(1);
                downloadSessionMock.SetupGet(m => m.FileSize).Returns(1000);
                downloadSessionMock.SetupGet(m => m.FileSize32).Returns(1000);
                return(downloadSessionMock.Object);
            })
            .Verifiable();

            // Set up the mock vault.
            var vaultMock = new Mock <Vault>();

            vaultMock
            .SetupGet(m => m.ObjectFileOperations)
            .Returns(vaultObjectFileOperationsMock.Object);

            // Act.
            var stream = new Extensions.FileDownloadStream(objectFileMock.Object, vaultMock.Object, fileFormat);

            stream.OpenDownloadSession();

            // Ensure object is populated.
            Assert.IsNotNull(stream.DownloadSession);

            // Ensure we got hit as expected.
            vaultMock.Verify();
            vaultObjectFileOperationsMock.Verify();
        }
 /// <summary>
 /// Opens a read-only stream to access the existing file contents.
 /// </summary>
 /// <param name="objectFile">The file to download.</param>
 /// <param name="vault">The vault to download from.</param>
 /// <param name="fileFormat">The format of file to request from server.</param>
 /// <returns>A <see cref="FileDownloadStream"/> that can be used to read the file from the vault.</returns>
 /// <remarks>Ensure that the stream is correctly closed and disposed of (e.g. with a <see langword="using"/> statement).</remarks>
 public static FileDownloadStream OpenRead
 (
     this ObjectFile objectFile,
     Vault vault,
     MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
 )
 {
     return(new FileDownloadStream(objectFile, vault, fileFormat));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a <see cref="FileDownloadStream"/> but does not open the download session.
 /// </summary>
 /// <param name="fileToDownload">The file to download.</param>
 /// <param name="vault">The vault to download from.</param>
 /// <param name="fileFormat">The format to request the file in from the server.</param>
 public FileDownloadStream(
     ObjectFile fileToDownload,
     Vault vault,
     MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
     )
 {
     // Set properties.
     this.FileToDownload = fileToDownload ?? throw new ArgumentNullException(nameof(fileToDownload));
     this.Vault          = vault ?? throw new ArgumentNullException(nameof(vault));
     this.FileFormat     = fileFormat;
 }
 /// <summary>
 /// Downloads the <paramref name="objectFile"/> from the <paramref name="vault"/>.
 /// </summary>
 /// <param name="objectFile">The file to download.</param>
 /// <param name="vault">The vault to download from.</param>
 /// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
 /// <param name="fileFormat">The format of file to request from server.</param>
 /// <returns>A <see cref="TemporaryFileDownload"/> representing the downloaded file.</returns>
 public TemporaryFileDownload DownloadFile
 (
     ObjectFile objectFile,
     Vault vault,
     int blockSize           = FileTransfers.DefaultBlockSize,
     MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
 )
 {
     return(objectFile.Download
            (
                vault,
                this.GenerateTemporaryFileInfo(objectFile),
                blockSize,
                fileFormat
            ));
 }
 /// <summary>
 /// Downloads the file to disk.
 /// </summary>
 /// <param name="objectFile">The file to download.</param>
 /// <param name="vault">The vault to download from.</param>
 /// <param name="filePath">The location on disk to download to.</param>
 /// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
 /// <param name="fileFormat">The format of file to request from server.</param>
 /// <returns>A <see cref="TemporaryFileDownload"/> representing the completed file download.</returns>
 public static TemporaryFileDownload Download
 (
     this ObjectFile objectFile,
     Vault vault,
     string filePath,
     int blockSize           = FileTransfers.DefaultBlockSize,
     MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
 )
 {
     return(objectFile
            .Download
            (
                vault,
                new FileInfo(filePath),
                blockSize,
                fileFormat
            ));
 }
        /// <summary>
        /// Downloads the file from the vault.
        /// </summary>
        /// <param name="overwriteExistingFiles">
        /// If the <see cref="TemporaryFileDownload.TargetFile"/> exists and this is true then it will be deleted prior to download starting.
        /// If it exists and this is false then an <see cref="InvalidOperationException"/> is thrown. </param>
        /// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
        /// <param name="fileFormat">The format of file to request from server.</param>
        public virtual void Download(
            bool overwriteExistingFiles = true,
            int blockSize           = FileTransfers.DefaultBlockSize,
            MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
            )
        {
            // If the block size is less than 1 then throw.
            if (blockSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(blockSize), "The block size must be a positive integer.");
            }

            // If the target file exists and overwriteExistingFiles is false then throw.
            if (this.TargetFile.Exists)
            {
                if (false == overwriteExistingFiles)
                {
                    throw new InvalidOperationException("File already exists");
                }

                // Exists, but we can overwrite it.
                this.TargetFile.Delete();
            }

            // Open the target file for writing.
            using (var stream = this.TargetFile.OpenWrite())
            {
                // Read the data as a stream.
                using (var downloadStream = new FileDownloadStream(this.FileToDownload, this.Vault, fileFormat))
                {
                    // Copy the downloaded data to the output stream.
                    downloadStream.CopyTo(stream, blockSize);
                }
            }

            // Re-hydrate the temporary file data.
            this.TargetFile = new FileInfo(this.TargetFile.FullName);
        }
        /// <summary>
        /// Creates a <see cref="TemporaryFileDownload"/> and downloads <paramref name="fileToDownload"/>
        /// from the <paramref name="vault"/> to <paramref name="downloadTo"/>.
        /// </summary>
        /// <param name="fileToDownload">The file to download.</param>
        /// <param name="vault">The vault to download from.</param>
        /// <param name="downloadTo">The location on disk to download to.</param>
        /// <param name="overwriteExistingFiles">If false and <paramref name="downloadTo"/> exists then an <see cref="InvalidOperationException"/> is thrown.</param>
        /// <param name="blockSize">The size of the block to use for file transfers (defaults to <see cref="TemporaryFileDownload.DefaultDownloadBlockSize"/>).</param>
        /// <param name="fileFormat">The format to request the file in from the server.</param>
        /// <returns></returns>
        public static TemporaryFileDownload Download(
            ObjectFile fileToDownload,
            Vault vault,
            FileInfo downloadTo,
            bool overwriteExistingFiles = true,
            int blockSize           = FileTransfers.DefaultBlockSize,
            MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
            )
        {
            // Create the download object.
            var download = new TemporaryFileDownload
                           (
                fileToDownload,
                vault,
                downloadTo
                           );

            // Download the file.
            download.Download(overwriteExistingFiles, blockSize, fileFormat);

            // Return the download object.
            return(download);
        }