예제 #1
0
        public async Task CopyFileAsync(string srcPath, string dstPath)
        {
            var physicalSrcPath = GetPhysicalPath(srcPath);

            if (await getGridFSFileInfoAsync(physicalSrcPath) == null)
            {
                throw new FileStoreException($"The file '{srcPath}' does not exist.");
            }

            var physicalDstPath = GetPhysicalPath(dstPath);

            if (await getGridFSFileInfoAsync(physicalDstPath) != null)
            {
                throw new FileStoreException($"Cannot copy file because the destination path '{dstPath}' already exists.");
            }


            var stream = await _bucket.OpenDownloadStreamByNameAsync(physicalSrcPath);

            var response = await _bucket.UploadFromStreamAsync(physicalDstPath, stream);

            if (response == null)
            {
                throw new FileStoreException($"Cannot copy file '{srcPath}'.");
            }
        }
        private async Task MigratePortraits(CancellationToken cancellationToken)
        {
            var source = new GridFSBucket(_source, new GridFSBucketOptions
            {
                BucketName = "portraits"
            });

            using (var cursor = await source.FindAsync(Builders <GridFSFileInfo> .Filter.Empty))
            {
                var files = await cursor.ToListAsync();

                var destination = new GridFSBucket(_destination, new GridFSBucketOptions
                {
                    BucketName = "portraits"
                });

                foreach (var file in files)
                {
                    using (var stream = await source.OpenDownloadStreamByNameAsync(file.Filename, null, cancellationToken))
                    {
                        await destination.UploadFromStreamAsync(file.Filename, stream, null, cancellationToken);
                    }
                }
            }
        }
예제 #3
0
        public async Task DemoDownloadFileAsync(IMongoDatabase database, string filePath, string fileName)
        {
            var gridFsBucket = new GridFSBucket(database);

            using (
                GridFSDownloadStream <ObjectId> sourceStream = await gridFsBucket.OpenDownloadStreamByNameAsync(fileName)
                )
            {
                using (FileStream destinationStream = File.Open(filePath, FileMode.Create))
                {
                    await sourceStream.CopyToAsync(destinationStream);
                }
            }
        }
예제 #4
0
        /// <inheritdoc />
        public async Task <Stream> Read(string id)
        {
            try
            {
                var downloadStream = await _bucket.OpenDownloadStreamByNameAsync(id);

                await UpdateLastReadTime(id, _rebusTime.Now);

                return(downloadStream);
            }
            catch (GridFSFileNotFoundException exception)
            {
                throw new ArgumentException($"Could not read attachment with ID '{id}'", exception);
            }
        }
예제 #5
0
        public async Task <Stream> Handle(GetPortraitQuery query, CancellationToken cancellationToken)
        {
            var bucket = new GridFSBucket(_database, new GridFSBucketOptions
            {
                BucketName = "portraits"
            });

            try
            {
                return(await bucket.OpenDownloadStreamByNameAsync(query.Name, null, cancellationToken));
            }
            catch (GridFSFileNotFoundException)
            {
                var      assembly = Assembly.GetExecutingAssembly();
                string[] names    = assembly.GetManifestResourceNames();
                Stream   resource = assembly.GetManifestResourceStream("DMWorkshop.Handlers.defaultcreature.jpg");
                return(resource);
            }
        }