public IDocumentId MakeDocument(IContentManagerArenaId arena, string filePath, FileUsingMode mode = FileUsingMode.AutoDetect, bool deleteSourceFile = false, string fileName = null) { if (arena is null) { throw new ArgumentNullException(nameof(arena)); } if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException(nameof(filePath)); } if (!File.Exists(filePath)) { throw new ArgumentException($"{nameof(filePath)} = '{filePath}' does not exists."); } fileName = fileName ?? Path.GetFileName(filePath); if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException(nameof(fileName) + " is empty."); } if (mode == FileUsingMode.AutoDetect) { mode = PathIsNetworkPath(filePath) ? FileUsingMode.CopySourceFile : FileUsingMode.UseSourceFile; } IDocumentId documentId = null; IContentProvider contentProvider = null; Action <IDocumentId> onCloseArena = OnCloseArenaDeleteDocument; switch (mode) { case FileUsingMode.UseSourceFile: { if (!deleteSourceFile) { onCloseArena = null; } documentId = new DocumentIdImpl(arena, filePath); } break; case FileUsingMode.CopySourceFile: { var arenaFilePath = Path.GetTempFileName(); if (deleteSourceFile) { File.Move(filePath, arenaFilePath, true); documentId = new DocumentIdImpl(arena, arenaFilePath); } else { contentProvider = new FileContentProvider(contentDestinationPath => File.Copy(filePath, contentDestinationPath, true), () => File.Length(filePath)); documentId = new DocumentIdImpl(arena, arenaFilePath); } } break; default: throw new NotImplementedException($"{nameof(mode)}={mode}"); } AddDocumentInfo(documentId, new DocumentInfo(fileName, onCloseArena, contentProvider)); return(documentId); }
public IDocumentId MakeLazyDocument(IContentManagerArenaId arena, string name, IContentProvider contentProvider) { if (contentProvider is null) { throw new ArgumentNullException(nameof(contentProvider)); } var documentId = new DocumentIdImpl(arena); AddDocumentInfo(documentId, new DocumentInfo(name, OnCloseArenaDeleteDocument, contentProvider)); return(documentId); }
public IDocumentId MakeDocument(IContentManagerArenaId arena, INamedStream document) { if (document is null) { throw new ArgumentNullException(nameof(document)); } if (document.Stream is null) { throw new ArgumentNullException(nameof(document) + "." + nameof(document.Stream)); } var documentId = new DocumentIdImpl(arena); using (var content = File.Open(documentId.DocumentId, FileMode.Open, FileAccess.Write, FileShare.None)) { //document.Stream.Flush(); document.Stream.CopyTo(content); //content.Flush(); }; AddDocumentInfo(documentId, new DocumentInfo(document.Name, OnCloseArenaDeleteDocument, null)); return(documentId); }