Starts in:
1 DAY
18 HRS
25 MIN
33 SEC
Starts in:
1 D
18 H
25 M
33 S
Open Closed

I want to allow Anonymous file upload #4812


User avatar
0
saad.aldulaijan created
  • ABP Framework version: v7.0.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

I am using FileManagementModule; and I want to allow anonymous uploads, how can i achieve this.

I tried overriding the following and it didn't work:

I tried to override the controller:

    [ExposeServices(typeof(FileDescriptorController))]
    [AllowAnonymous]
    public class MyFileDescriptorController : FileDescriptorController
    {
        public MyFileDescriptorController(IFileDescriptorAppService fileDescriptorAppService) : base(fileDescriptorAppService)
        {
        }


        [AllowAnonymous]
        [HttpPost]
        [Route("upload")]
        public override Task<FileDescriptorDto> CreateAsync(Guid? directoryId, CreateFileInputWithStream inputWithStream)
        {
            return FileDescriptorAppService.CreateAsync(directoryId, inputWithStream);
        }

    }

overriding the app service


[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IFileDescriptorAppService), typeof(FileDescriptorAppService), typeof(MyFileDescriptorAppService))]

[AllowAnonymous]
public class MyFileDescriptorAppService : FileDescriptorAppService
{
    public MyFileDescriptorAppService(IFileManager fileManager,
        IFileDescriptorRepository fileDescriptorRepository,
        IBlobContainer<FileManagementContainer> blobContainer,
        IDistributedCache<FileDownloadTokenCacheItem, string> downloadTokenCache)
        : base(fileManager, fileDescriptorRepository, blobContainer, downloadTokenCache)
    {
    }


    [AllowAnonymous]
    public override async Task<FileDescriptorDto> CreateAsync(Guid? directoryId, CreateFileInputWithStream inputWithStream)
    {
        await CheckSizeAsync(inputWithStream.File.ContentLength ?? 0);

        var fileDescriptor = await FileManager.CreateAsync(inputWithStream.Name, inputWithStream.File.ContentType, inputWithStream.File, directoryId, CurrentTenant.Id, overrideExisting: true);

        await CurrentUnitOfWork.SaveChangesAsync();

        fileDescriptor = await FileDescriptorRepository.FindAsync(fileDescriptor.Id);
        inputWithStream.MapExtraPropertiesTo(fileDescriptor);
        await FileDescriptorRepository.UpdateAsync(fileDescriptor);
        return ObjectMapper.Map<FileDescriptor, FileDescriptorDto>(fileDescriptor);

    }
}

3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try this:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(FileDescriptorController), IncludeSelf = true)]
    [AllowAnonymous]
    public class MyFileDescriptorController : FileDescriptorController
    {
        public MyFileDescriptorController(IFileDescriptorAppService fileDescriptorAppService) : base(fileDescriptorAppService)
        {
        }
    
    
        [AllowAnonymous]
        [HttpPost]
        [Route("upload")]
        public override Task CreateAsync(Guid? directoryId, CreateFileInputWithStream inputWithStream)
        {
            return FileDescriptorAppService.CreateAsync(directoryId, inputWithStream);
        }
    
    }
    

    Document: https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Overriding-Services#example-overriding-a-controller

  • User Avatar
    0
    saad.aldulaijan created

    I already tried it it didn't work, it returns 401 if not authenticated. It seems fileDescriptorAppService itself is secured.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    It works for me:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(FileDescriptorController), IncludeSelf = true)]
    [AllowAnonymous]
    public class MyFileDescriptorController : FileDescriptorController
    {
        public MyFileDescriptorController(IFileDescriptorAppService fileDescriptorAppService) : base(fileDescriptorAppService)
        {
        }
    
    
        [AllowAnonymous]
        [HttpPost]
        [Route("upload")]
        public override Task<FileDescriptorDto> CreateAsync(Guid? directoryId, CreateFileInputWithStream inputWithStream)
        {
            return FileDescriptorAppService.CreateAsync(directoryId, inputWithStream);
        }
    }
    
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IFileDescriptorAppService), typeof(FileDescriptorAppService), typeof(MyFileDescriptorAppService))]
    [AllowAnonymous]
    public class MyFileDescriptorAppService : FileDescriptorAppService
    {
        public MyFileDescriptorAppService(IFileManager fileManager,
            IFileDescriptorRepository fileDescriptorRepository,
            IBlobContainer<FileManagementContainer> blobContainer,
            IDistributedCache<FileDownloadTokenCacheItem, string> downloadTokenCache)
            : base(fileManager, fileDescriptorRepository, blobContainer, downloadTokenCache)
        {
        }
    
    
        [AllowAnonymous]
        public override async Task<FileDescriptorDto> CreateAsync(Guid? directoryId, CreateFileInputWithStream inputWithStream)
        {
            return new FileDescriptorDto()
            {
                Name = "OK"
            };
        }
    }
    

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06