示例#1
0
        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);
        }
示例#2
0
        public static Stream Open()
        {
            var innerStream = File.Open(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileOptions.DeleteOnClose);

            try
            {
                var outerStream = new BufferedStream(innerStream, BufferSize);
                try
                {
                    return(new NotFlushingBufferOnDisposeStream(outerStream, innerStream));
                }
                catch
                {
                    outerStream.Dispose();
                    throw;
                }
            }
            catch
            {
                innerStream?.Dispose();
                throw;
            }
        }