/// <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> /// Adds a new file to the specified object. /// </summary> /// <param name="objectFileOperations">The instance of <see cref="VaultObjectFileOperations"/> to use.</param> /// <param name="objVer">The object version to add the file to. Must already be checked out.</param> /// <param name="vault">The vault to add the file to.</param> /// <param name="title">The title of the file (without an extension).</param> /// <param name="extension">The file extension. Can be supplied with or without preceeding ".".</param> /// <param name="fileContents">The contents of the file.</param> public static FileVer AddFile ( this VaultObjectFileOperations objectFileOperations, ObjVer objVer, Vault vault, string title, string extension, Stream fileContents ) { // Sanity. if (null == objectFileOperations) { throw new ArgumentNullException(nameof(objectFileOperations)); } if (null == objVer) { throw new ArgumentNullException(nameof(objVer)); } if (null == vault) { throw new ArgumentNullException(nameof(vault)); } if (String.IsNullOrWhiteSpace(title)) { throw new ArgumentException("The file must have a title/name.", nameof(title)); } if (null == fileContents) { throw new ArgumentNullException(nameof(fileContents)); } if (false == fileContents.CanRead) { throw new ArgumentException("The file contents must represent a readable stream.", nameof(fileContents)); } extension = extension ?? string.Empty; if (extension.StartsWith(".")) { extension = extension.Substring(1); } // Add the empty file. var fileVer = vault .ObjectFileOperations .AddEmptyFile ( objVer, title, extension ); // Set the file contents. using (var uploadStream = new FileUploadStream(fileVer, objVer.ObjID, vault)) { fileContents.CopyTo(uploadStream); } // Return the new file version data. return(fileVer); }
/// <summary> /// Accesses a file's contents as a stream for writing. /// </summary> /// <param name="objectFileOperations">The instance of <see cref="VaultObjectFileOperations"/> to use.</param> /// <param name="objectFile">The file to open for writing.</param> /// <param name="vault">The vault the file comes from.</param> /// <param name="automaticallyCommitOnDisposal">If true, will automatically save the file contents to the vault when this stream is disposed of.</param> /// <returns>The file opened as a write-only <see cref="Stream"/>.</returns> /// <remarks>Use <see cref="OpenFileForWriting(VaultObjectFileOperations, ObjectFile, ObjID, Vault, bool)"/> if you have the <see cref="ObjID"/> as this is more efficient.</remarks> public static Stream OpenFileForWriting ( this VaultObjectFileOperations objectFileOperations, ObjectFile objectFile, Vault vault, bool automaticallyCommitOnDisposal = true ) { // 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 FileUploadStream ( objectFile.FileVer, vault.ObjectFileOperations.GetObjIDOfFile(objectFile.ID), vault, automaticallyCommitOnDisposal )); }
/// <summary> /// Adds a new file to the specified object. /// </summary> /// <param name="objectFileOperations">The instance of <see cref="VaultObjectFileOperations"/> to use.</param> /// <param name="objVerEx">The object version to add the file to. Must already be checked out.</param> /// <param name="title">The title of the file (without an extension).</param> /// <param name="extension">The file extension. Can be supplied with or without preceeding ".".</param> /// <param name="fileContents">The contents of the file.</param> public static void AddFile ( this VaultObjectFileOperations objectFileOperations, ObjVerEx objVerEx, string title, string extension, Stream fileContents ) { // Sanity. if (null == objectFileOperations) { throw new ArgumentNullException(nameof(objectFileOperations)); } if (null == objVerEx) { throw new ArgumentNullException(nameof(objVerEx)); } if (null == objVerEx.Vault) { throw new ArgumentException("The ObjVerEx does not have a valid vault connection.", nameof(objVerEx)); } if (String.IsNullOrWhiteSpace(title)) { throw new ArgumentException("The file must have a title/name.", nameof(title)); } if (null == fileContents) { throw new ArgumentNullException(nameof(fileContents)); } // Use the other extension method. objectFileOperations.AddFile ( objVerEx.ObjVer, objVerEx.Vault, title, extension, fileContents ); }