private string GetContentType(string path)
        {
            ContentTypeProvider.TryGetContentType(path, out var contentType);
            if (string.IsNullOrWhiteSpace(contentType))
            {
                return("application/octet-stream");
            }

            return(contentType);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> DownloadAsync(string directory, string filename)
        {
            FileSystemEntity entity = null;

            switch (directory)
            {
            case "AuditLogs":
                entity = Repository.AuditLogs.GetFileByFilename(filename);
                break;
            }

            if (entity == null)
            {
                return(NotFound());
            }

            var contentType = ContentTypeProvider.TryGetContentType(entity.Filename, out string type) ? type : "application/octet-stream";

            return(File(entity.Content, contentType));
        }
Exemplo n.º 3
0
        public async Task <bool> Handle(CreateOrUpdateDocumentCommand request, CancellationToken cancellationToken = default)
        {
            var path = request.Path;

            if (await _documentRepository.IsContainerPath(path.ToString()))
            {
                throw new InvalidOperationException();
            }

            // ドキュメントの存在チェック
            var document = await _documentRepository.GetAsync(path.ToString());

            // ファイル情報の取得
            try
            {
                if (!ContentTypeProvider.TryGetContentType(path.Extension, out var contentType))
                {
                    contentType = "application/octet-stream";
                }
                if (document == null)
                {
                    var utcNow = DateTime.UtcNow;
                    document = new Document(
                        new DocumentPath(path.ToString()),
                        contentType,
                        request.Data,
                        request.Created ?? utcNow,
                        request.LastModified ?? request.Created ?? utcNow);
                    await _documentRepository.AddAsync(document).ConfigureAwait(false);
                }
                else if (request.ForceCreate)
                {
                    var retryCount = 0;
                    var dirPath    = path.DirectoryPath;
                    var fnwoe      = path.FileNameWithoutExtension;
                    var ext        = path.Extension;
                    var filename   = path;
                    while (document != null)
                    {
                        retryCount++;
                        filename = dirPath.Combine(fnwoe + $"({retryCount})" + ext);
                        document = await _documentRepository.GetAsync(filename.ToString());
                    }

                    var utcNow = DateTime.UtcNow;
                    document = new Document(
                        new DocumentPath(filename.ToString()),
                        contentType,
                        request.Data,
                        request.Created ?? utcNow,
                        request.LastModified ?? request.Created ?? utcNow);
                    await _documentRepository.AddAsync(document).ConfigureAwait(false);
                }
                else
                {
                    var utcNow = DateTime.UtcNow;

                    if (request.Data.Hash == document.Hash)
                    {
                        await _dataStore.DeleteAsync(request.Data.StorageKey).ConfigureAwait(false);

                        if (request.Created != document.Created ||
                            request.LastModified != document.LastModified ||
                            contentType != document.ContentType)
                        {
                            var oldData = await _dataStore.FindAsync(document.StorageKey);

                            if (oldData == null)
                            {
                                document.Update(
                                    contentType,
                                    request.Data,
                                    request.Created ?? document.Created,
                                    request.LastModified ?? utcNow);
                            }
                            else
                            {
                                document.Update(
                                    contentType,
                                    oldData,
                                    request.Created ?? document.Created,
                                    request.LastModified ?? utcNow);
                            }
                        }
                    }
                    else
                    {
                        document.Update(
                            contentType,
                            request.Data,
                            request.Created ?? document.Created,
                            request.LastModified ?? utcNow);
                    }
                    await _documentRepository.UpdateAsync(document);
                }
                await _documentRepository.UnitOfWork.SaveEntitiesAsync().ConfigureAwait(false);

                return(true);
            }
            catch
            {
                await _dataStore.DeleteAsync(request.Data.StorageKey).ConfigureAwait(false);

                throw;
            }
        }