trannamtrung1st / elFinder.Net.Core

An elFinder backend connector with less magic code and more compatibility. This enables .NET Standard 2.0 projects to easily integrate elFinder functionalities.
Apache License 2.0
12 stars 8 forks source link

Customize uploadingFileName #30

Closed benny-adiwijaya closed 2 years ago

benny-adiwijaya commented 2 years ago

Hi, is it posible to customize uploadingFileName without overriding UploadAsync, so the server can control file name that uploaded by user?

trannamtrung1st commented 2 years ago

Hi @benny-adiwijaya , set a custom collection of files to the ConnectorCommand.Files property should be fine. You can overwrite the IFormFileWrapper or create one. please take a look into the source code to see how it works.

benny-adiwijaya commented 2 years ago

Hi @trannamtrung1st, I was not successful to set a custom collection of files. Can you give little sample how to do it?

trannamtrung1st commented 2 years ago

This is not built-in but you can try the below code. It may be changed in the future, so be careful since this is not supported in elFinder specs.

        [Route("connector")]
        public async Task<IActionResult> Connector()
        {
            await SetupConnectorAsync();
            var cmd = ConnectorHelper.ParseCommand(Request);
            var ccTokenSource = ConnectorHelper.RegisterCcTokenSource(HttpContext);

            if (cmd.Cmd == ConnectorCommand.Cmd_Upload)
            {
                // Case 1: for chunking upload
                if (cmd.Args.TryGetValue(ConnectorCommand.Param_Chunk, out var chunk) && chunk.ToString().Length > 0)
                {
                    var isChunkMerge = !cmd.Args.TryGetValue(ConnectorCommand.Param_Cid, out var cid) || cid.ToString().Length == 0;
                    if (isChunkMerge)
                    {
                        var overrideForm = new Dictionary<string, StringValues>(cmd.Form);
                        overrideForm[ConnectorCommand.Param_Upload] = Guid.NewGuid().ToString();
                        cmd.Form = overrideForm;
                    }
                }
                else
                {
                    // Case 2: for non-chunking upload
                    cmd.Files = cmd.Files?.Select(f =>
                        new FormFileWrapper(f.OpenReadStream(), f.Length, f.Name, Guid.NewGuid().ToString())); // custom name here
                }
            }

            var conResult = await _connector.ProcessAsync(cmd, ccTokenSource);
            var actionResult = conResult.ToActionResult(HttpContext);
            return actionResult;
        }
benny-adiwijaya commented 2 years ago

Nice, it works. Thank you