/// <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> /// Replaces an existing file with data from another stream. /// </summary> /// <param name="objectFile">The file to replace.</param> /// <param name="vault">The vault from in which the file exists.</param> /// <param name="objectId">The object to which this file belongs.</param> /// <param name="input">The new content for the file.</param> /// <param name="blockSize">The block size to use.</param> public static void ReplaceFileContent ( this ObjectFile objectFile, Vault vault, ObjID objectId, Stream input, int blockSize = FileTransfers.DefaultBlockSize ) { // Sanity. if (null == objectFile) { throw new ArgumentNullException(nameof(objectFile)); } if (null == vault) { throw new ArgumentNullException(nameof(vault)); } if (null == objectId) { throw new ArgumentNullException(nameof(objectId)); } if (null == input) { throw new ArgumentNullException(nameof(input)); } if (false == input.CanRead) { throw new ArgumentException("The input stream is not readable.", nameof(input)); } // Documentation says block size must be between 1KB and 4MB. if (blockSize < FileTransfers.MinimumUploadBlockSize) { blockSize = FileTransfers.MinimumUploadBlockSize; } if (blockSize > FileTransfers.MaximumUploadBlockSize) { blockSize = FileTransfers.MaximumUploadBlockSize; } // Create the (write-only) upload stream. using (var uploadStream = new FileUploadStream(objectFile.FileVer, objectId, vault)) { // Copy the input stream to the upload stream. input.CopyTo(uploadStream, blockSize); } }