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);
}
}
}
Aby zmodyfikować komponent Vue.js edit-medical-staff-profile.vue tak, aby umożliwiał pobieranie pliku za pomocą dwóch różnych linków SAS (z różnymi Content-Disposition), należy wprowadzić kilka zmian. Poniżej przedstawiam szczegółowy krok po kroku sposób implementacji tego rozwiązania.
1. Zrozumienie Wymagań
Masz istniejący backend, który generuje link SAS z Content-Disposition ustawionym na inline. Chcesz, aby front-end generował dodatkowy link SAS z Content-Disposition ustawionym na attachment, umożliwiając użytkownikowi zarówno przeglądanie pliku w przeglądarce, jak i jego pobieranie.
2. Modyfikacja Metody downloadImage
Będziemy modyfikować metodę downloadImage w komponencie Vue.js, aby generowała dwa linki SAS:
Link 1: Z Content-Disposition ustawionym na inline (obecny link).
Link 2: Z Content-Disposition ustawionym na attachment.
Aby to osiągnąć, skorzystamy z istniejącego linku SAS, a następnie dodamy do niego odpowiednią część zapytania, aby zmienić nagłówek Content-Disposition.
3. Implementacja Zmian w Komponencie Vue.js
a. Dodanie Metody getAttachmentSasLink
Dodajemy pomocniczą metodę, która modyfikuje istniejący link SAS, dodając parametr response-content-disposition=attachment.
typescript
// edit-medical-staff-profile.vue
<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";
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");
};
// Pomocnicza metoda do generowania linku SAS z attachment
const getAttachmentSasLink = (sasUrl: string, fileName: string): string => {
const url = new URL(sasUrl);
// Usunięcie istniejących parametrów Content-Disposition, jeśli istnieją
url.searchParams.delete("response-content-disposition");
// Dodanie nowego parametru Content-Disposition dla attachment
url.searchParams.append("response-content-disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
return url.toString();
};
const downloadImage = async () => {
try {
// Pobranie SAS linku z backendu
const response = await axios.get(`/api/files/GetSasLink/${model.value.avatarFileId}`, {
responseType: 'json'
});
const sasUrl = response.data.url; // Zakładamy, że backend zwraca { url: '...' }
// Generowanie linku z Content-Disposition: inline
const inlineUrl = sasUrl;
// Generowanie linku z Content-Disposition: attachment
const attachmentUrl = getAttachmentSasLink(sasUrl, model.value.avatarFileName);
// Opcja 1: Otwórz obraz w nowej karcie (inline)
window.open(inlineUrl, '_blank');
// Opcja 2: Pobierz plik jako załącznik (attachment)
const link = document.createElement('a');
link.href = attachmentUrl;
link.download = model.value.avatarFileName; // Nazwa pliku do pobrania
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
console.error("Error downloading image:", error);
notifier.errorToast("Błąd podczas pobierania obrazu.");
}
};
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>
b. Aktualizacja Obsługi Pobierania
Zmodyfikowana metoda downloadImage wykonuje następujące kroki:
Pobiera Pierwszy Link SAS: Uzyskuje link SAS z backendu, który ma Content-Disposition ustawiony na inline.
Generuje Drugi Link SAS: Korzysta z pierwszego linku SAS i dodaje do niego parametr response-content-disposition=attachment, co powoduje, że przeglądarka pobierze plik zamiast go wyświetlać.
Inicjalizuje Obie Akcje:
Otwiera pierwszy link w nowej karcie (wyświetla obraz).
Automatycznie pobiera plik jako załącznik.
c. Dostosowanie Backendowego API
Zakładamy, że masz endpoint API /api/files/GetSasLink/{id}, który zwraca link SAS w formacie JSON, na przykład:
Upewnij się, że frontend poprawnie interpretuje odpowiedź.
d. Obsługa Modyfikacji Nazwy Pliku
W metodzie getAttachmentSasLink przekazujemy nazwę pliku, aby ustawić odpowiednią nazwę podczas pobierania. Upewnij się, że masz takie pola jak avatarFileId i avatarFileName w swoim modelu. Jeśli ich nie masz, musisz je dodać.
4. Dodatkowe Ulepszenia
a. Dodanie Oddzielnych Przycisków dla Różnych Opcji Pobierania
Możesz dodać dwa osobne przyciski, jeden do wyświetlania obrazu w przeglądarce, a drugi do jego pobierania jako załącznik.
html
<template>
<!-- ...pozostała część szablonu... -->
<v-btn
v-if="model.avatarUrl"
color="primary"
class="mt-4"
@click="downloadImageInline">
Wyświetl w przeglądarce
</v-btn>
<v-btn
v-if="model.avatarUrl"
color="secondary"
class="mt-4 ml-2"
@click="downloadImageAttachment">
Pobierz
</v-btn>
<!-- ...pozostała część szablonu... -->
</template>
Następnie w sekcji <script> dodaj dwie oddzielne metody:
Bezpieczeństwo: Upewnij się, że linki SAS są generowane w sposób bezpieczny i mają odpowiednią datę ważności (ExpiresOn). Unikaj długiego czasu ważności, aby zminimalizować ryzyko nadużyć.
Obsługa Błędów: Implementacja powinna uwzględniać obsługę różnych scenariuszy błędów, takich jak brak pliku, problemy z siecią itp.
Testowanie: Przetestuj nowe funkcjonalności dokładnie, aby upewnić się, że oba linki SAS działają zgodnie z oczekiwaniami i że Content-Disposition jest poprawnie ustawiony.
Refaktoryzacja: Jeśli masz więcej miejsc, w których potrzebujesz różnych linków SAS, rozważ stworzenie globalnego serwisu lub helpera w frontendzie, który będzie zarządzał generowaniem i manipulowaniem linkami SAS.
6. Podsumowanie
Wprowadzenie dwóch różnych linków SAS umożliwia elastyczne zarządzanie sposobem prezentacji plików: użytkownik może zdecydować się na ich otwarcie w przeglądarce lub pobranie na dysk. Dzięki manipulacji linkiem SAS na frontendzie bez konieczności zmiany backendu, osiągnąłeś zamierzony cel zgodny z założeniami projektowymi.
7. Cały Zaktualizowany Kod Komponentu
Dla pełnej jasności, poniżej przedstawiam cały zaktualizowany komponent edit-medical-staff-profile.vue:
vue
8. Wnioski
Dzięki powyższym zmianom użytkownik będzie mógł zarówno przeglądać obraz w przeglądarce, jak i pobierać go jako załącznik, korzystając z dwóch różnych linków SAS z odpowiednio ustawionymi nagłówkami Content-Disposition. Kluczowym elementem jest manipulacja parametrami URL SAS w frontendzie, co pozwala na osiągnięcie wymaganego zachowania bez konieczności modyfikowania backendu.
Jeśli masz dodatkowe pytania lub potrzebujesz dalszej pomocy, daj znać!