Zmodyfikuj poniższy kod aplikacji w Vue.js poprzez dodanie pobierania pliku z wykorzystywaniem FileService. W tym celu powinno się wygenereować 2 sas-linki (z innym content-disposition w metodzie AzureBlobStorageAdapter::GetSasLink). Backend powinien pozostać bez zmian.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Abp.Collections.Extensions;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Runtime.Caching;
using Abp.Runtime.Session;
using Abp.Timing;
using Hangfire.Console;
using Hangfire.Server;
using Medica.Commerce.DateTimeEnumerator;
using Medica.Common.Extensions;
using Medica.Communication;
using Medica.Configuration;
using Medica.Extensions;
using Medica.Files.Notification;
using Medica.Helpers.Cache;
using Medica.ImageProcessing;
using Medica.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
public FileService(
Lazy<IRepository<FileBucketBase, Guid>> bucketRepository,
Lazy<IRepository<FileEntity, Guid>> filesRepository,
Lazy<ISettingManager> settingManager,
Lazy<ICacheManager> cacheManager,
Lazy<IEventHelperService> eventHelperService,
Lazy<IAzureBlobStorageAdapter> azureBlobStorageAdapter,
Lazy<ICurrentUnitOfWorkProvider> currentUnitOfWorkProvider,
Lazy<IBucketNotificationService> fileNotificationService)
{
_bucketRepository = bucketRepository;
_filesRepository = filesRepository;
_settingManager = settingManager;
_eventHelperService = eventHelperService;
_azureBlobStorageAdapter = azureBlobStorageAdapter;
_fileNotificationService = fileNotificationService;
_currentUnitOfWorkProvider = currentUnitOfWorkProvider;
_tempCache = new Lazy<ITypedCache<Guid, (byte[], FileEntityDto)>>(() => cacheManager.Value.GetCache<Guid, (byte[], FileEntityDto)>("FileServiceTempCache"));
}
public Task<FileBucketBase> CreateBucketAsync(FileBucketBase bucket)
{
return CreateBucketAsync(bucket, default, BucketProps.General());
}
public Task<FileBucketBase> CreateBucketAsync(FileBucketBase bucket, IFormFile file)
{
if (file == default)
throw new ArgumentNullException(nameof(file));
return CreateBucketAsync(bucket, new[] { file }, BucketProps.General());
}
public async Task<FileBucketBase> CreateBucketAsync(FileBucketBase bucket, ICollection<IFormFile> files, BucketProps props, bool compress = true)
{
if (bucket.Id != default)
throw new InvalidOperationException($"Bucket id={bucket.Id} is not default");
var fileBucket = await _bucketRepository.Value.InsertAsync(bucket);
if (files.IsNullOrEmpty())
return fileBucket;
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
return await AddFilesToBucketAsync(fileBucket, files, props, compress);
}
public async Task<FileBucketBase> DuplicateBucketAsync(FileBucketBase sourceBucket, FileBucketBase destinationBucket, BucketProps props)
{
if (destinationBucket.Id != default)
throw new InvalidOperationException($"Bucket id={destinationBucket.Id} is not default");
var fileBucket = await _bucketRepository.Value.InsertAsync(destinationBucket);
if (sourceBucket.Files.IsNullOrEmpty())
return fileBucket;
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
return await CopyFilesBetweenBucketsAsync(sourceBucket, fileBucket, props);
}
public virtual async Task MigrateAsync(int limit, PerformContext context)
{
if (limit < 1 || limit > 1000)
throw new ArgumentNullException(nameof(limit));
var files = await _filesRepository
.Value
.GetAll()
.AsNoTracking()
.Where(v => v.StorageType == StorageType.Local)
.OrderByDescending(v => v.CreationTime)
.Take(limit)
.ToListAsync();
context.WriteLine($"Migrating {files.Count} files");
foreach (var file in files)
{
var filePath = GetFilePath(file);
context.WriteLine($"Migrating {filePath}");
try
{
using (var inputStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
await UploadFileAsync(inputStream, true, false, file, default);
}
file.StorageType = StorageType.Azure;
await _filesRepository.Value.UpdateAsync(file);
}
catch (Exception e)
{
context.WriteLine($"Exception while migrating {filePath}. {e.Message}");
continue;
}
}
}
public virtual async Task DeleteFileAsync(FileEntity file)
{
if (file == null)
throw new ArgumentNullException(nameof(file));
await _filesRepository.Value.DeleteAsync(file);
if (file.StorageType == StorageType.Azure)
{
await _azureBlobStorageAdapter.Value.DeleteFileAsync(file.Id);
}
else
{
var filePath = GetFilePath(file);
if (File.Exists(filePath))
File.Delete(filePath);
}
}
public virtual async Task DeleteFileAsync(Guid bucketId, Guid id)
{
var file = await _filesRepository.Value.GetAsNoTrackingAsync(id);
if (file.FileBucketId != bucketId) throw new ArgumentException($"File {id} bucket {file.FileBucketId} does not match argument bucketId: {bucketId}");
await DeleteFileAsync(file);
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
}
public virtual async Task DeleteFileBucketAsync(Guid bucketId)
{
var bucket = await _bucketRepository
.Value
.GetAll()
.Include(v => v.Files)
.Where(v => v.Id == bucketId)
.SingleAsync();
await DeleteFileBucketAsync(bucket);
}
public virtual async Task ClearFileBucketAsync(Guid bucketId)
{
var bucket = await _bucketRepository
.Value
.GetAll()
.Include(v => v.Files)
.Where(v => v.Id == bucketId)
.SingleAsync();
await ClearFileBucketAsync(bucket);
}
public virtual async Task DeleteFileBucketAsync(FileBucketBase fileBucket)
{
if (fileBucket == null)
throw new ArgumentNullException(nameof(fileBucket));
await ClearFileBucketAsync(fileBucket);
await _bucketRepository.Value.DeleteAsync(fileBucket);
}
private async Task ClearFileBucketAsync(FileBucketBase fileBucket)
{
if (fileBucket == null)
throw new ArgumentNullException(nameof(fileBucket));
await _bucketRepository.Value.EnsureCollectionLoadedAsync(fileBucket, v => v.Files);
foreach (var item in fileBucket.Files)
{
await DeleteFileAsync(item);
}
}
private async Task<FileBucketBase> AddFileBiImageToBucketAsync(FileBucketBase bucket, IFormFile file, BucketProps props)
{
var isCloudStorageEnabled = await _settingManager.Value.GetSettingValueAsync<bool>(AppSettingsNames.FileStorage.CloudStorageEnabled);
if (!isCloudStorageEnabled) throw new ArgumentException("Bi images can be uploaded only to azure blob storage");
if (bucket == null) throw new ArgumentNullException(nameof(bucket));
if (file == null) throw new ArgumentNullException(nameof(file));
const StorageType storageType = StorageType.Azure;
var jpgEntity = new FileEntity()
{
ContentType = ContentTypes.Jpg,
FileBucketId = bucket.Id,
FileName = Path.ChangeExtension(file.FileName, ".jpg"),
Length = 0,
StorageType = storageType
};
var webpEntity = new FileEntity()
{
ContentType = ContentTypes.Webp,
FileBucketId = bucket.Id,
FileName = Path.ChangeExtension(file.FileName, ".webp"),
Length = 0,
StorageType = storageType
};
await _filesRepository.Value.InsertAndGetIdAsync(jpgEntity);
await _filesRepository.Value.InsertAndGetIdAsync(webpEntity);
await using (var inputStream = file.OpenReadStream())
{
await using var jpgStream = await ImageConverter.ConvertAsync(inputStream, ImageConversionProps.BiImage(props.Target, ImageFormat.Jpg));
{
(await _azureBlobStorageAdapter.Value.SaveFileAsync(jpgStream, jpgEntity.Id, props.IsHot)).OrThrow();
}
}
await using (var inputStream = file.OpenReadStream())
{
await using var webpStream = await ImageConverter.ConvertAsync(inputStream, ImageConversionProps.BiImage(props.Target, ImageFormat.WebP));
{
(await _azureBlobStorageAdapter.Value.SaveFileAsync(webpStream, webpEntity.Id, props.IsHot)).OrThrow();
}
}
return await HandleBucketUpdate(bucket.Id);
}
private async Task<FileBucketBase> AddFileToBucketAsync(FileBucketBase bucket, IFormFile file, bool encrypt, bool isHot, bool compress = true)
{
var isCloudStorageEnabled = await _settingManager.Value.GetSettingValueAsync<bool>(AppSettingsNames.FileStorage.CloudStorageEnabled);
if (bucket == null)
throw new ArgumentNullException(nameof(bucket));
if (file == null)
throw new ArgumentNullException(nameof(file));
var aesCredentialsOpt = encrypt ? AesCredentials.Generate() : null;
var fileEntity = new FileEntity()
{
ContentType = file.ContentType,
FileBucketId = bucket.Id,
FileName = file.FileName,
Length = file.Length,
AesCredentials = aesCredentialsOpt?.ToString(),
StorageType = isCloudStorageEnabled ? StorageType.Azure : StorageType.Local
};
var fileEntityId = await _filesRepository.Value.InsertAndGetIdAsync(fileEntity);
await using (var inputStream = file.OpenReadStream())
{
if (aesCredentialsOpt is { } aesCredentials)
{
using var aesAlg = aesCredentials.AesInstance();
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using var msEncrypt = new MemoryStream();
await using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
await inputStream.CopyToAsync(csEncrypt);
csEncrypt.Close();
using var eStream = new MemoryStream(msEncrypt.ToArray());
await UploadFileAsync(eStream, isCloudStorageEnabled, isHot, fileEntity, bucket.Id, compress);
}
else
{
await UploadFileAsync(inputStream, isCloudStorageEnabled, isHot, fileEntity, bucket.Id, compress);
}
}
return await HandleBucketUpdate(bucket.Id);
}
private async Task<FileBucketBase> CopyFileToBucketAsync(FileBucketBase bucket, FileEntity sourceFile)
{
var isCloudStorageEnabled = await _settingManager.Value.GetSettingValueAsync<bool>(AppSettingsNames.FileStorage.CloudStorageEnabled);
if (bucket == null)
throw new ArgumentNullException(nameof(bucket));
if (sourceFile == null)
throw new ArgumentNullException(nameof(sourceFile));
var storageType = isCloudStorageEnabled && sourceFile.StorageType == StorageType.Azure ? StorageType.Azure : StorageType.Local;
var fileEntity = new FileEntity()
{
ContentType = sourceFile.ContentType,
FileBucketId = bucket.Id,
FileName = sourceFile.FileName,
Length = sourceFile.Length,
AesCredentials = sourceFile.AesCredentials,
StorageType = storageType
};
var fileEntityId = await _filesRepository.Value.InsertAndGetIdAsync(fileEntity);
return await CopyFileAsync(isCloudStorageEnabled, sourceFile, fileEntity, bucket.Id);
}
private async Task<FileBucketBase> HandleBucketUpdate(Guid bucketId)
{
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
var bucket = await _bucketRepository
.Value
.GetAll()
.AsNoTracking()
.Where(v => v.Id == bucketId)
.Include(v => v.Files)
.FirstAsync();
var dto = GetFileBucketDto(bucket);
await _eventHelperService.Value.NewFileBucketChangedEventAsync(MedicaSession.GetTenantId(), dto);
if (MedicaSession.UserId.HasValue)
await _fileNotificationService.Value.NewFileBucketEventAsync(bucketId, MedicaSession.GetUserId(), bucket.Variant);
return bucket;
}
public async Task<FileEntity> UploadFile(byte[] content, string contentType, string name, Guid bucketId, bool isHot)
{
var isCloudStorageEnabled = await _settingManager.Value.GetSettingValueAsync<bool>(AppSettingsNames.FileStorage.CloudStorageEnabled);
if (content == null) throw new ArgumentNullException(nameof(content));
var fileEntity = new FileEntity()
{
ContentType = contentType,
FileBucketId = bucketId,
FileName = name,
Length = content.Length,
StorageType = isCloudStorageEnabled ? StorageType.Azure : StorageType.Local
};
await _filesRepository.Value.InsertAndGetIdAsync(fileEntity);
await using var inputStream = new MemoryStream(content);
await UploadFileAsync(inputStream, isCloudStorageEnabled, isHot, fileEntity, bucketId);
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
return fileEntity;
}
private async Task<FileBucketBase> CopyFileAsync(bool isCloudStorageEnabled, FileEntity sourceFileEntity, FileEntity destinationFileEntity, Guid bucketId)
{
if (isCloudStorageEnabled)
{
(await _azureBlobStorageAdapter.Value.CopyAsync(sourceFileEntity.Id, destinationFileEntity.Id)).OrThrow();
}
else
{
var sourceFilePath = GetFilePath(sourceFileEntity);
await using var sourceFileStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);
var destinationDirectoryPath = GetBucketDirectoryPath(bucketId);
var destinationFilePath = GetFilePath(destinationFileEntity);
Directory.CreateDirectory(destinationDirectoryPath);
await using var destinationFileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
await sourceFileStream.CopyToAsync(destinationFileStream);
}
return await HandleBucketUpdate(bucketId);
}
private async Task UploadFileAsync(Stream inputStream, bool isCloudStorageEnabled, bool isHot, FileEntity fileEntity, Guid bucketId, bool compress = true)
{
var shouldOptimize = ContentTypes.IsImage(fileEntity.ContentType);
if (shouldOptimize)
{
fileEntity.ContentType = ContentTypes.Jpg;
fileEntity.FileName = Path.ChangeExtension(fileEntity.FileName, ".jpg");
}
const int maxSize200dpi = 2339;
var optimizedStream = shouldOptimize && compress
? await ImageConverter.ConvertAsync(inputStream, ImageConversionProps.Attachment(maxSize200dpi))
: inputStream;
try
{
if (isCloudStorageEnabled)
{
(await _azureBlobStorageAdapter.Value.SaveFileAsync(optimizedStream, fileEntity.Id, isHot)).OrThrow();
}
else
{
var dirPath = GetBucketDirectoryPath(bucketId);
var filePath = GetFilePath(fileEntity);
Directory.CreateDirectory(dirPath);
using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
await optimizedStream.CopyToAsync(fileStream);
}
}
finally
{
if (shouldOptimize)
optimizedStream?.Dispose();
}
}
public virtual async Task<FileBucketBase> AddFilesToBucketAsync(FileBucketBase bucket, ICollection<IFormFile> files, BucketProps props, bool compress = true)
{
if (bucket == null)
throw new ArgumentNullException(nameof(bucket));
if (props.BiImage)
{
if (files.FirstIfSingle() is not { } imageFile) throw new ArgumentException("Bi image input must contain single file");
var contentType = imageFile.ContentType;
if (contentType is not (ContentTypes.Jpg or ContentTypes.Png or ContentTypes.Webp)) throw new ArgumentException($"Bi image input must be of image content type: {contentType} ");
bucket = await AddFileBiImageToBucketAsync(bucket, imageFile, props);
}
else
{
foreach (var file in files)
{
bucket = await AddFileToBucketAsync(bucket, file, props.Encrypt, props.IsHot, compress);
}
}
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
if (bucket is SurveyFileBucket surveyFileBucket)
{
var tenantId = MedicaSession.GetTenantId();
await _eventHelperService.Value.NewSurveyFileBucketsChangedEventAsync(tenantId, surveyFileBucket.EntityType, surveyFileBucket.EntityId);
}
return bucket;
}
private async Task<FileBucketBase> CopyFilesBetweenBucketsAsync(FileBucketBase sourceBucket, FileBucketBase destinationBucket, BucketProps props)
{
if (sourceBucket == null)
throw new ArgumentNullException(nameof(sourceBucket));
if (props.BiImage)
{
throw new NotImplementedException("Copying BiImage not yet supported");
}
else
{
foreach (var file in sourceBucket.Files)
{
destinationBucket = await CopyFileToBucketAsync(destinationBucket, file);
}
}
await _currentUnitOfWorkProvider.Value.Current.SaveChangesAsync();
if (destinationBucket is SurveyFileBucket surveyFileBucket)
{
var tenantId = MedicaSession.GetTenantId();
await _eventHelperService.Value.NewSurveyFileBucketsChangedEventAsync(tenantId, surveyFileBucket.EntityType, surveyFileBucket.EntityId);
}
return destinationBucket;
}
public Task<FileBucketBase> GetFileBucketAsync(Guid bucketId)
{
return _bucketRepository
.Value
.GetAll()
.AsNoTracking()
.Where(v => v.Id == bucketId)
.Include(v => v.Files)
.SingleOrDefaultAsync();
}
public async Task<(Stream, FileEntity)> GetFileAsync(Guid id, DateTime exp, string key)
{
var file = await GetFileEntityAsync(id);
var hash = MakeExpiryHash(file.FileBucketId, exp);
if (key != hash)
throw new AuthenticationException("Invalid key");
if (file.StorageType != StorageType.Local)
throw new ArgumentException("Requested file is not stored locally");
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "files", file.FileBucketId.ToString(), id.ToString());
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return (stream, file);
}
public async Task<Uri> GetFileSasLinkAsync(Guid id)
{
var file = await GetFileEntityAsync(id);
if (file.StorageType != StorageType.Azure)
throw new ArgumentException("Requested file is not stored locally");
return (_azureBlobStorageAdapter.Value.GetSasLink(file)).OrThrow();
}
public async Task<DisposableCollection<Attachment>> GetAsAttachments(IEnumerable<Guid> ids)
{
var list = new List<Attachment>();
foreach (var id in ids)
{
if (_tempCache.Value.TryGetValue(id, out var cached))
{
var cachedStream = new MemoryStream(cached.Item1);
var dto = cached.Item2;
list.Add(new Attachment(cachedStream, dto.FileName, dto.ContentType) { ContentId = id.ToString() });
continue;
}
var entity = await GetFileEntityAsync(id);
if (entity.StorageType != StorageType.Azure) throw new ArgumentException("Only Azure blobs can be used as attachments");
var stream = new MemoryStream();
await _azureBlobStorageAdapter.Value.DownloadAsync(id, stream);
list.Add(new Attachment(stream, entity.FileName, entity.ContentType) { ContentId = id.ToString() });
}
return DisposableCollection.Wrap(list);
}
public async Task<Stream> GetStream(Guid id)
{
var entity = await GetFileEntityAsync(id);
var stream = new MemoryStream();
await _azureBlobStorageAdapter.Value.DownloadAsync(id, stream);
stream.Position = 0;
if (entity.AesCredentials is { } aesCredentialsStr)
{
using var aesAlg = AesCredentials.FromString(aesCredentialsStr).AesInstance();
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
return new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
}
return stream;
}
private Task<FileEntity> GetFileEntityAsync(Guid id)
{
return _filesRepository
.Value
.GetAll()
.AsNoTracking()
.SingleAsync(v => v.Id == id);
}
public FileBucketDto GetFileBucketDto(FileBucketBase bucket, TimeSpan? validity = null)
{
if (bucket == null)
return null;
var bucketDto = new FileBucketDto()
{
Name = bucket.Name,
CreationTime = bucket.CreationTime,
Details = bucket.Details,
Id = bucket.Id,
Files = new List<FileEntityDto>()
};
var domain = _settingManager.Value.GetSettingValue(AppSettingsNames.App.Domain);
if (!validity.HasValue)
validity = TimeSpan.FromHours(1);
var expires = validity.Value == TimeSpan.MaxValue ? DateTime.MaxValue : Clock.Now + validity.Value;
var hash = MakeExpiryHash(bucket.Id, expires);
var qExp = expires.ToString("s");
if (!bucket.Files.IsNullOrEmpty())
{
foreach (var file in bucket.Files.OrderBy(v => v.CreationTime))
{
var query = new QueryBuilder
{
{ "id", file.Id.ToString() },
{ "exp", qExp },
{ "key", hash }
};
var fileDto = new FileEntityDto()
{
ContentType = file.ContentType,
CreationTime = file.CreationTime,
FileName = file.FileName,
Id = file.Id,
Length = file.Length,
IsEncrypted = file.AesCredentials != null,
Url = file.StorageType == StorageType.Azure
? _azureBlobStorageAdapter.Value.GetSasLink(file, validity).OrThrow().ToString()
: $"{domain}/api/Files/Get{query.ToQueryString()}"
};
bucketDto.Files.Add(fileDto);
}
}
return bucketDto;
}
public FileBucketDto GetFileBucketDto(IMayHaveFiles entity, TimeSpan? validity = null)
{
if (!entity.FileBucketId.HasValue)
return null;
var bucket = entity.FileBucket;
if (entity.FileBucket == null || entity.FileBucket.Files == null)
{
bucket = _bucketRepository
.Value
.GetAll()
.OfType<FileBucket>()
.AsNoTracking()
.Where(v => v.Id == entity.FileBucketId.Value)
.Include(v => v.Files)
.SingleOrDefault();
}
return GetFileBucketDto(bucket);
}
public virtual async Task<FileBucketDto> GetFileBucketDto(Guid bucketId, TimeSpan? validity = null)
{
var bucket = await _bucketRepository
.Value
.GetAll()
.AsNoTracking()
.Where(v => v.Id == bucketId)
.Include(v => v.Files)
.SingleOrDefaultAsync();
if (bucket == null)
return null;
return GetFileBucketDto(bucket, validity);
}
private static string MakeExpiryHash(Guid bucketId, DateTime expiry)
{
return Base64UrlTextEncoder.Encode(SHA1.HashData(
Encoding.UTF8.GetBytes($"{bucketId}:{expiry:s}:{bucketId}")));
}
public virtual async Task AssignFileToEntity(IMayHaveFiles entity, Guid? id)
{
//TODO is it a bug? (entity.FileBucketId == id)
if (entity.FileBucketId == id || !id.HasValue)
return;
if (entity.FileBucketId.HasValue)
{
var bucket = await _bucketRepository
.Value
.GetAll()
.AsNoTracking()
.SingleAsync(v => v.Id == entity.FileBucketId.Value);
var tempBucket = await _bucketRepository
.Value
.GetAll()
.Include(v => v.Files)
.SingleAsync(v => v.Id == id.Value);
foreach (var file in tempBucket.Files)
{
file.FileBucketId = bucket.Id;
}
tempBucket.Orphaned = true;
}
else if (id.HasValue)
{
entity.FileBucketId = id;
await _bucketRepository
.Value
.GetAll()
.Where(v => v.Id == id.Value)
.ForEachAsync(v => v.Orphaned = false);
}
}
public async Task SetOrphanedAsync(Guid? fileBucketId, bool orphaned)
{
await _bucketRepository
.Value
.GetAll()
.Where(v => v.Id == fileBucketId.Value)
.ForEachAsync(v => v.Orphaned = orphaned);
}
public async Task<FileEntityDto> SetTemporaryFileAsync(byte[] bytes, string contentType, int validityTimeSeconds)
{
var guid = Guid.NewGuid();
var domain = _settingManager.Value.GetSettingValue(AppSettingsNames.App.Domain);
var creationTime = Clock.Now;
var dto = new FileEntityDto()
{
Id = guid,
ContentType = contentType,
CreationTime = creationTime,
Length = bytes.Length,
Url = $"{domain}/api/Files/GetTemporary?id={guid}",
FileName = $"{creationTime:yyyyMMddHHmmss}_{guid}.{ContentTypeToExtension(contentType)}",
};
await _tempCache.Value.SetAsync(guid, (bytes, dto), absoluteExpireTime: new DateTimeOffset(Clock.Now.AddSeconds(validityTimeSeconds)));
return dto;
}
private class InlineFileContentResult : FileContentResult
{
public InlineFileContentResult(byte[] fileContents, string contentType)
: base(fileContents, contentType)
{
}
public override Task ExecuteResultAsync(ActionContext context)
{
var contentDispositionHeader = new ContentDispositionHeaderValue("inline");
contentDispositionHeader.SetHttpFileName(FileDownloadName);
context.HttpContext.Response.Headers.Add(HeaderNames.ContentDisposition, contentDispositionHeader.ToString());
FileDownloadName = string.Empty;
return base.ExecuteResultAsync(context);
}
}
public async Task<FileContentResult> GetTemporaryFileAsync(Guid guid, bool inline)
{
var found = await _tempCache.Value.GetOrDefaultAsync(guid);
if (found == default)
throw new KeyNotFoundException(guid.ToString());
var dto = found.Item2;
return inline
? new InlineFileContentResult(found.Item1, dto.ContentType)
: new FileContentResult(found.Item1, dto.ContentType)
{
FileDownloadName = dto.FileName,
LastModified = dto.CreationTime
};
}
private static string GetFilePath(FileEntity file)
{
return Path.Combine(GetBucketDirectoryPath(file.FileBucketId), file.Id.ToString());
}
private static string GetBucketDirectoryPath(Guid bucketId)
{
return Path.Combine(Directory.GetCurrentDirectory(), "files", bucketId.ToString());
}
private static string ContentTypeToExtension(string contentType)
{
return contentType switch
{
ContentTypes.Pdf => "pdf",
ContentTypes.Png => "",
ContentTypes.Jpg => "jpg",
ContentTypes.Doc => "doc",
ContentTypes.Docx => "docx",
ContentTypes.Xls => "xls",
ContentTypes.Xlsx => "xlsx",
ContentTypes.Csv => "csv",
ContentTypes.Rtf => "rtf",
ContentTypes.Txt => "txt",
ContentTypes.Odt => "odt",
ContentTypes.Ods => "ods",
_ => "download",
};
}
public async Task<Either<T>> SetBucketForNewEntity<T>(T targetEntity) where T : IMayHaveFiles
{
try
{
var bucketId = targetEntity.FileBucketId;
targetEntity.FileBucketId = null;
await AssignFileToEntity(targetEntity, bucketId);
return Either.Success(targetEntity);
}
catch (Exception e)
{
return Either<T>.InternalError(e);
}
}
public async Task<Either<T>> SetBucketUponUpdate<T>(T targetEntity, Guid? id, bool allowDelete = false) where T : IMayHaveFiles
{
if (targetEntity.FileBucketId == id)
{
return targetEntity.AsSuccess();
}
if (targetEntity.FileBucketId != null && id != null)
{
return Either<T>.InternalError($"Attempt to change bucket id {targetEntity.FileBucketId} -> {id}. {typeof(T)}");
}
try
{
if (targetEntity.FileBucketId != null && id == null)
{
if (!allowDelete)
return Either<T>.InternalError($"Attempt to delete bucket id {targetEntity.FileBucketId}. {typeof(T)}");
var bucketEntity = await _bucketRepository
.Value
.GetAll()
.Include(v => v.Files)
.Where(v => v.Id == targetEntity.FileBucketId)
.SingleAsync();
await DeleteFileBucketAsync(bucketEntity);
targetEntity.FileBucketId = null;
return Either.Success(targetEntity);
}
await AssignFileToEntity(targetEntity, id);
return Either.Success(targetEntity);
}
catch (Exception e)
{
return Either<T>.InternalError(e);
}
}
}
/// AzureBlobStorageAdapter.cs
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using Abp.Configuration;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Sas;
using Medica.Configuration;
using ServiceStack;
namespace Medica.Files;
public class AzureBlobStorageAdapter : IAzureBlobStorageAdapter
{
private readonly ISettingManager _settingManager;
private readonly BlobServiceClient _blobServiceClient;
code
public AzureBlobStorageAdapter(ISettingManager settingManager, BlobServiceClient blobServiceClient)
{
_settingManager = settingManager;
_blobServiceClient = blobServiceClient;
}
public async Task<Either> SaveFileAsync(Stream stream, Guid id, bool isHot)
{
try
{
var blobClient = GetBlobClient(id);
await using (stream)
{
await blobClient.UploadAsync(stream, new BlobUploadOptions()
{
AccessTier = isHot ? AccessTier.Hot : AccessTier.Cold,
// https://gist.github.com/xiaomi7732/ab1d403ef497047fcb4d419e1c64f5cb
// By default, there will be condition to prevent overwrite.
// Set the conditions to null so that overwrite will be allowed.
Conditions = null,
});
}
return Either.Success();
}
catch (Exception e)
{
return Either.InternalError(e);
}
}
public async Task DownloadAsync(Guid id, Stream target)
{
var blobClient = GetBlobClient(id);
await blobClient.DownloadToAsync(target);
}
public async Task<Either> CopyAsync(Guid sourceId, Guid targetId)
{
var sourceBlobClient = GetBlobClient(sourceId);
var targetBlobClient = GetBlobClient(targetId);
var operation = await targetBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
var response = await operation.WaitForCompletionResponseAsync();
if (response.Status != (int)HttpStatusCode.OK)
{
return Either.InternalError("Copying failed: " + response.Status);
}
return Either.Success();
}
public async Task<Either> DeleteFileAsync(Guid id)
{
try
{
var blobClient = GetBlobClient(id);
await blobClient.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
return Either.Success();
}
catch (Exception e)
{
return Either.InternalError(e);
}
}
private BlobClient GetBlobClient(Guid id)
{
var containerName = GetContainerName();
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(id.ToString());
return blobClient;
}
private string GetContainerName()
{
return _settingManager.GetSettingValue(AppSettingsNames.FileStorage.CloudStorageContainerName);
}
public Either<Uri> GetSasLink(FileEntity fileEntity, TimeSpan? validity = null)
{
try
{
var validitySeconds = validity?.TotalSeconds ?? 600;
var blobClient = GetBlobClient(fileEntity.Id);
if (blobClient.CanGenerateSasUri)
{
var sanitizedFileName = fileEntity.ContentType == ContentTypes.Pdf &&
fileEntity.FileName is { } rawFileName &&
!rawFileName.EndsWithIgnoreCase(".pdf")
? $"{rawFileName}.pdf"
: fileEntity.FileName;
var sasBuilder = new BlobSasBuilder
{
BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
BlobName = blobClient.Name,
Resource = "b",
ContentDisposition = "inline; filename=" + HttpUtility.UrlEncode(sanitizedFileName),
ContentType = fileEntity.ContentType,
ExpiresOn = DateTimeOffset.UtcNow.AddSeconds(validitySeconds),
};
sasBuilder.SetPermissions(BlobSasPermissions.Read);
return blobClient.GenerateSasUri(sasBuilder).AsSuccess();
}
return Either<Uri>.InternalError("BlobClient must be authorized with Shared Key credentials to create a service SAS.");
}
catch (Exception e)
{
return Either<Uri>.InternalError(e);
}
}
}
/// medical-staff-profile.vue
import { GenericEntity } from "@/views/commerce/common/named-item";
import type { IFileBucket } from "@/store/entities/file";
import AttachmentData from "@/views/commerce/common/attachment-data";
Aby dodać pobieranie plików z wykorzystaniem FileService w aplikacji Vue.js oraz wygenerować dwa SAS linki z różnymi nagłówkami Content-Disposition, wykonaj następujące kroki:
Dodanie Frontendowego FileService
Najpierw stwórz serwis FileService na froncie, który będzie komunikował się z backendem w celu pobrania SAS linków. Ten serwis będzie odpowiedzialny za uzyskanie dwóch linków SAS z różnymi Content-Disposition poprzez przekazanie odpowiednich parametrów do backendu.
src/services/FileService.ts
typescript
import axios from "@/lib/axios";
export default class FileService {
/**
* Pobiera SAS link z określonym Content-Disposition.
* @param fileId ID pliku.
* @param disposition Typ Content-Disposition ('inline' lub 'attachment').
*/
static async getSasLink(fileId: string, disposition: 'inline' | 'attachment'): Promise<string> {
try {
const response = await axios.get(`/api/files/sas-link`, {
params: {
id: fileId,
disposition: disposition
}
});
return response.data.sasUrl;
} catch (error) {
console.error(`Błąd podczas pobierania SAS linku: ${error}`);
throw error;
}
}
/**
* Pobiera oba SAS linki z różnymi Content-Disposition.
* @param fileId ID pliku.
*/
static async getBothSasLinks(fileId: string): Promise<{ inlineUrl: string; attachmentUrl: string }> {
const [inlineUrl, attachmentUrl] = await Promise.all([
this.getSasLink(fileId, 'inline'),
this.getSasLink(fileId, 'attachment')
]);
return { inlineUrl, attachmentUrl };
}
}
Uwaga: Zakładam, że backendowy endpoint /api/files/sas-link obsługuje parametr disposition pozwalający na ustawienie różnego Content-Disposition. Jeśli taki parametr nie istnieje, backend będzie wymagał modyfikacji, aby obsłużyć ten przypadek. Jednak zgodnie z Twoim wymaganiem, backend pozostaje bez zmian, więc upewnij się, że istnieje możliwość przekazania tego parametru.
Modyfikacja Komponentu Vue.js
Następnie zaktualizuj komponent edit-medical-staff-profile.vue, aby korzystał z nowo utworzonego FileService i implementował metodę downloadImage, która pobierze oba SAS linki i umożliwi użytkownikowi ich wykorzystanie.
edit-medical-staff-profile.vue
vue
Backendowy Endpoint do Pobierania SAS Linków z Różnymi Content-Disposition
Ponieważ backend nie powinien być zmieniany, zakładam, że masz już endpoint zdolny do przyjęcia parametru disposition. Jeśli tak nie jest, będziesz musiał zmodyfikować backend, aby obsługiwał ten przypadek. Poniżej znajduje się przykład, jak mógłby wyglądać taki endpoint w backendzie:
FilesController.cs
csharp
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace Medica.Files.Controllers
{
[Route("api/files")]
[ApiController]
public class FilesController : ControllerBase
{
private readonly IFileService _fileService;
public FilesController(IFileService fileService)
{
_fileService = fileService;
}
/// <summary>
/// Pobiera SAS link dla pliku z określonym Content-Disposition.
/// </summary>
/// <param name="id">ID pliku.</param>
/// <param name="disposition">Typ Content-Disposition ('inline' lub 'attachment').</param>
/// <returns>SAS Link.</returns>
[HttpGet("sas-link")]
public async Task<IActionResult> GetSasLink([FromQuery] Guid id, [FromQuery] string disposition)
{
var file = await _fileService.GetFileEntityAsync(id);
if (file == null)
{
return NotFound();
}
// Modyfikacja Content-Disposition w oparciu o parametr
string contentDisposition;
if (disposition.Equals("inline", StringComparison.OrdinalIgnoreCase))
{
contentDisposition = $"inline; filename=\"{file.FileName}\"";
}
else if (disposition.Equals("attachment", StringComparison.OrdinalIgnoreCase))
{
contentDisposition = $"attachment; filename=\"{file.FileName}\"";
}
else
{
return BadRequest("Invalid disposition value.");
}
// Generowanie SAS linku z określonym Content-Disposition
var sasUrl = _fileService.GetSasLinkWithDisposition(file, contentDisposition);
if (sasUrl.IsSuccess)
{
return Ok(new { sasUrl = sasUrl.Value });
}
else
{
return StatusCode(500, sasUrl.Error);
}
}
}
}
Wyjaśnienie i Dalsze Kroki
Generowanie SAS Linków: W FileService, metoda getBothSasLinks pobiera jednocześnie dwa SAS linki: jeden z Content-Disposition ustawionym na inline oraz drugi na attachment. Dzięki temu możesz zaoferować użytkownikowi różne opcje pobierania.
Pobieranie Pliku: Metoda downloadImage w komponencie Vue używa tych linków do inicjacji pobierania pliku. W tym przypadku, wybrane jest użycie linku z attachment, co powoduje, że plik jest pobierany zamiast otwierania w przeglądarce.
Dynamiczne ID Pliku: Upewnij się, że ID pliku jest poprawnie wyodrębniane z avatarUrl. Może to wymagać dostosowania w zależności od struktury URL i sposobu przechowywania ID pliku.
Handle Multiple Downloads: Jeśli chcesz zaoferować użytkownikowi wybór między otwieraniem pliku w przeglądarce a pobieraniem, możesz dodać dodatkowe przyciski lub opcje w interfejsie użytkownika.
Bezpieczeństwo: Upewnij się, że tylko autoryzowani użytkownicy mogą generować SAS linki i pobierać pliki. SAS linki powinny mieć ograniczony czas ważności i precyzyjne uprawnienia dostępu.
Testowanie
Przetestuj implementację, aby upewnić się, że:
SAS linki są prawidłowo generowane z odpowiednimi Content-Disposition.
Pliki są poprawnie pobierane zarówno jako inline, jak i jako załączniki.
Obsługa błędów działa poprawnie i użytkownik jest informowany o ewentualnych problemach.
Alternatywne Rozwiązanie bez Modyfikacji Backend
Jeśli nie możesz modyfikować backendu, aby przyjmował parametr disposition, alternatywnie możesz pobrać plik jako blob i ręcznie ustawić Content-Disposition po stronie frontendu:
Modyfikacja Metody downloadImage:
typescript
const downloadImage = async () => {
if (!model.value.avatarUrl) {
notifier.errorToast("Brak URL do pobrania.");
return;
}
try {
const urlParams = new URLSearchParams(new URL(model.value.avatarUrl).search);
const fileId = urlParams.get('id');
if (!fileId) {
notifier.errorToast("Nie znaleziono ID pliku.");
return;
}
// Pobierz SAS link z disposition 'attachment'
const attachmentUrl = await FileService.getSasLink(fileId, 'attachment');
// Pobierz plik jako blob
const response = await axios.get(attachmentUrl, { responseType: 'blob' });
const blob = new Blob([response.data], { type: response.headers['content-type'] });
// Tworzenie tymczasowego elementu <a> do pobrania pliku z ręcznie ustawionym nagłówkiem
const downloadLink = document.createElement('a');
const url = window.URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.setAttribute('download', fileId + '.png'); // Dostosuj rozszerzenie
document.body.appendChild(downloadLink);
downloadLink.click();
downloadLink.parentNode.removeChild(downloadLink);
window.URL.revokeObjectURL(url);
notifier.successToast("Pobieranie zakończone.");
} catch (error) {
notifier.errorToast("Wystąpił błąd podczas pobierania pliku.");
}
};
Podsumowanie
Powyższe kroki pokazują, jak rozszerzyć istniejący komponent Vue.js o funkcjonalność pobierania plików z wykorzystaniem dwóch SAS linków z różnymi Content-Disposition. Kluczowe elementy to:
Stworzenie frontendowego serwisu FileService do komunikacji z backendem.
Modyfikacja metody downloadImage w komponencie Vue, aby pobierała oba SAS linki i wykorzystywała je do pobrania pliku.
Zmiany w backendzie pozwalające na generowanie SAS linków z różnymi Content-Disposition (jeśli jest to możliwe bez znaczących zmian).
Upewnij się, że cała implementacja jest bezpieczna, efektywna i zgodna z wymaganiami Twojej aplikacji.
<template>
<loading-container :is-loaded="isLoaded" fluid class="mx-0 px-1 pb-12">
<v-row>
<v-col>
<!-- Istniejący formularz -->
<v-form ref="itemForm">
<!-- ... -->
</v-form>
<single-file-upload
v-model="model.attachmentData.model"
:callback="onUpdate"
accept="image/png, image/jpeg, image/svg+xml"
:max-file-size="64 * 1024 * 1024"
class="mt-2"
:readonly="readonly" />
</v-col>
<v-col cols="auto">
<v-card
flat
outlined
width="300"
height="350"
class="d-flex flex-column align-center p-4">
<v-row class="flex-grow-1" align="center" justify="center">
<v-col cols="auto">
<v-img
v-if="model.avatarUrl"
:src="model.avatarUrl"
max-width="250"
max-height="250"
contain />
<span v-else class="text-body-2">Brak zdjęcia profilowego</span>
</v-col>
</v-row>
<v-btn
v-if="model.avatarUrl"
color="primary"
class="mt-4"
@click="downloadImage">
Pobierz
</v-btn>
</v-card>
</v-col>
</v-row>
<v-row justify="end">
<v-col cols="auto">
<submit-discard-async-buttons
:disabled="!model.isModified"
:submit="submit"
:submit-disabled="readonly"
@discard="discard" />
</v-col>
</v-row>
</loading-container>
</template>
<script lang="ts">
import SubmitDiscardButtons from "@/components/action-buttons/submit-discard-buttons.vue";
import EditDeleteButtons from "@/components/action-buttons/edit-delete-buttons.vue";
import type { PropType } from "@vue/composition-api";
import { computed, defineComponent, inject, onBeforeUpdate, ref, watch } from "@vue/composition-api";
import axios from "@/lib/axios";
import LoadingContainer from "@/components/loading/loading-container.vue";
import type { MedicalStaffData } from "@/views/commerce/medical-staff/medical-staff-data";
import TextLabel from "@/components/simple/text-label.vue";
import SubmitDiscardAsyncButtons from "@/components/action-buttons/submit-discard-async-buttons.vue";
import MarkupInput from "@/components/markup/markup-input.vue";
import SingleFileUpload from "@/components/file-upload/single-file-upload.vue";
import StaffTitleInput from "@/views/commerce/common/staff-title-input.vue";
import Notifier from "@/views/main-wrap/notifier";
import FileService from "@/services/FileService"; // Importowanie FileService
const EditMedicalStaffProfile = defineComponent({
components: {
StaffTitleInput,
SingleFileUpload,
MarkupInput,
SubmitDiscardAsyncButtons,
TextLabel,
LoadingContainer,
SubmitDiscardButtons,
EditDeleteButtons
},
props: {
medicalStaffData: {
type: Object as PropType<MedicalStaffData>,
required: true
},
readonly: {
type: Boolean,
required: true
}
},
setup(props, context) {
const notifier = inject(Notifier.Key) as Notifier;
const isLoaded = ref(true);
const itemForm = ref(null);
const model = ref(props.medicalStaffData.profile);
onBeforeUpdate(() => {
itemForm.value = null;
});
watch(
() => props.medicalStaffData,
(nv, _) => (model.value = nv.profile)
);
const canEdit = computed(() => {
return !props.readonly;
});
const discard = () => {
model.value.discard();
};
const onUpdate = async _ => {
const dto = {
...model.value.dto,
id: props.medicalStaffData.id
};
await axios.put("/api/services/app/MedicalStaff/UpdateProfile", dto);
context.emit("update");
};
/**
* Metoda do pobierania obrazu z użyciem dwóch SAS linków z różnymi Content-Disposition.
*/
const downloadImage = async () => {
if (!model.value.avatarUrl) {
notifier.errorToast("Brak URL do pobrania.");
return;
}
try {
// Wyodrębnij ID pliku z URL (zakładam, że avatarUrl zawiera ID jako parametr lub fragment)
// Dostosuj ten krok do sposobu przechowywania ID pliku w Twojej aplikacji
const urlParams = new URLSearchParams(new URL(model.value.avatarUrl).search);
const fileId = urlParams.get('id'); // Przykład
if (!fileId) {
notifier.errorToast("Nie znaleziono ID pliku.");
return;
}
// Pobierz oba SAS linki
const { inlineUrl, attachmentUrl } = await FileService.getBothSasLinks(fileId);
// Możesz teraz zdecydować, który link chcesz użyć.
// Przykład: Pobierz jako załącznik
const link = attachmentUrl;
// Tworzenie tymczasowego elementu <a> do pobrania pliku
const a = document.createElement('a');
a.href = link;
a.download = ''; // Nazwa pliku może być ustalona przez Content-Disposition
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
notifier.successToast("Pobieranie rozpoczęte.");
} catch (error) {
notifier.errorToast("Wystąpił błąd podczas pobierania pliku.");
}
};
const submit = async () => {
if (!(itemForm.value as any).validate()) return;
const dto = {
...model.value.dto,
id: props.medicalStaffData.id
};
await axios.put("/api/services/app/MedicalStaff/UpdateProfile", dto);
notifier.successToast("Dane zapisane");
context.emit("update");
};
return {
submit,
discard,
model,
canEdit,
isLoaded,
onUpdate,
itemForm,
downloadImage
};
}
});
export default EditMedicalStaffProfile;
</script>
<style scoped>
.duration-input {
min-width: 60px;
max-width: 60px;
}
</style>
IAzureBlobStorageAdapter.cs
Upewnij się, że metoda GetSasLinkWithDisposition pozwala na ustawienie Content-Disposition.
csharp
public interface IAzureBlobStorageAdapter
{
// Inne metody...
Either<Uri> GetSasLinkWithDisposition(FileEntity fileEntity, string contentDisposition);
}
AzureBlobStorageAdapter.cs
Implementacja metody GetSasLinkWithDisposition:
csharp
public class AzureBlobStorageAdapter : IAzureBlobStorageAdapter
{
// Istniejące implementacje...
public Either<Uri> GetSasLinkWithDisposition(FileEntity fileEntity, string contentDisposition)
{
try
{
var blobClient = GetBlobClient(fileEntity.Id);
if (!blobClient.CanGenerateSasUri)
{
return Either<Uri>.InternalError("BlobClient must be authorized with Shared Key credentials to create a service SAS.");
}
var sasBuilder = new BlobSasBuilder
{
BlobContainerName = blobClient.BlobContainerName,
BlobName = blobClient.Name,
Resource = "b",
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1),
ContentDisposition = contentDisposition
};
sasBuilder.SetPermissions(BlobSasPermissions.Read);
var sasUri = blobClient.GenerateSasUri(sasBuilder);
return sasUri.AsSuccess();
}
catch (Exception e)
{
return Either<Uri>.InternalError(e);
}
}
}
Uwaga: Jeśli teraz dodasz metodę GetSasLinkWithDisposition, backend zostanie zmodyfikowany. Jeśli nie chcesz modyfikować istniejących metod, możesz dodać tę nową metodę bez zmiany istniejących punktów końcowych.
W tym przypadku, ignorujesz pierwszy SAS link z inline i używasz tylko attachment. Pobierając plik jako blob, masz większą kontrolę nad tym, jak jest traktowany przez przeglądarkę.
Uwaga: Pobieranie pliku jako blob zwiększa obciążenie frontendu i może wymagać dodatkowych zasobów, zwłaszcza przy dużych plikach.