rpaschoal / ng-chat

💬 A simple facebook/linkedin lookalike chat module for Angular applications.
MIT License
155 stars 92 forks source link

UploadFile - restriction details and response check not available #115

Closed Code-N-K closed 5 years ago

Code-N-K commented 5 years ago

As file upload is used how can we stop file loader if any invalid response is their, and how to validate the response in UploadFile of Homecontroller. @rpaschoal

As a use-case i dont want to upload zip,exe,rar on any restricted format.

rpaschoal commented 5 years ago

Hi @Code-N-K ,

I've just released 2.0.2 with a couple bug fixes, and now when an error occurs while uploading a file the state of the file upload input gets reset.

If you want to validate for file extensions, you could do so now on your file processing API.

There is no way at the moment to capture the error and show a message to the user or a toast notification. I believe the best way to tackle this would be to add another method to the file upload interface so you can supply your implementation of what to do when an error occurs.

I am considering a change on the main adapter and see if I can update it from being an abstract class to an interface, if so I'll include the changes for the file upload adapter on the same release.

Code-N-K commented 5 years ago

I have added some code updates to file processing api.

public async Task UploadFile(IFormFile file, [FromForm(Name = "ng-chat-participant-id")] string userId) { if (userId != null && file != null) { //Limit 2mb size if (file.Length < 2097152) { if (file.FileName != null && !string.Equals(file.ContentType, "application/x-zip-compressed", StringComparison.OrdinalIgnoreCase) && !string.Equals(file.ContentType, "application/octet-stream", StringComparison.OrdinalIgnoreCase)&& !string.Equals(file.ContentType, "text/javascript", StringComparison.OrdinalIgnoreCase)) { // Storing file in temp path var filePath = Path.Combine(Path.GetTempPath(), file.FileName);

                    if (file.Length > 0)
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }
                    }

                    var baseUri = new Uri($"{Request.Scheme}://{Request.Host}{Request.PathBase}");
                    var fileUri = new Uri(baseUri, $"Uploads/{file.FileName}");

                    return Ok(new
                    {
                        type = 2, // MessageType.File = 2
                                  //fromId: ngChatSenderUserId, fromId will be set by the angular component after receiving the http response
                        toId = userId,
                        message = file.FileName,
                        mimeType = file.ContentType,
                        fileSizeInBytes = file.Length,
                        downloadUrl = fileUri.ToString()
                    });

                }
                else
                {
                    //return BadRequest(new { message = "Invalid file." });

                    return Ok(new
                    {
                        type = 0,
                        toId = "",
                        message = "Invalid file.",
                        mimeType = "",
                        fileSizeInBytes = "",
                        downloadUrl = ""
                    });
                }
            }
            else
            {
                //return BadRequest(new { message = "Invalid file size, only 2MB allowed." });

                return Ok(new
                {
                    type = 0,
                    toId = "",
                    message = "Invalid file size, only 2MB allowed.",
                    mimeType = "",
                    fileSizeInBytes = "",
                    downloadUrl = ""
                });
            }
        }
        else
        {
            //Return bad request//Should be Update
            /*
            return Ok(new
            {
                type = 0,
                toId = "",
                message = "no file",
                mimeType = "",
                fileSizeInBytes = "",
                downloadUrl = ""
            });
            */

            if (userId == null)
            {
                //return BadRequest(new { message = "userid not available." });
                return Ok(new
                {
                    type = 0,
                    toId = "",
                    message = "userid not available.",
                    mimeType = "",
                    fileSizeInBytes = "",
                    downloadUrl = ""
                });
            }
            else
            {
                //return BadRequest(new { message = "No file available." });

                return Ok(new
                {
                    type = 0,
                    toId = "",
                    message = "No file available.",
                    mimeType = "",
                    fileSizeInBytes = "",
                    downloadUrl = ""
                });
            }
        }
    }
rpaschoal commented 5 years ago

Hi @Code-N-K ,

Please read my detailed message above. There isn't support at the moment and I will investigate adding an event raised method to the file upload adapter contract.

I always encourage everyone to contribute to ng-chat so feel free to contribute to the main repository too with pull requests. Just make sure we discuss and detail the design for new features as we need to consider many use case scenarios for other people using ng-chat.

Cheers!

Code-N-K commented 5 years ago

Hi @Code-N-K ,

Please read my detailed message above. There isn't support at the moment and I will investigate adding an event raised method to the file upload adapter contract.

I always encourage everyone to contribute to ng-chat so feel free to contribute to the main repository too with pull requests. Just make sure we discuss and detail the design for new features as we need to consider many use case scenarios for other people using ng-chat.

Cheers!

Thats what i have written the code for my use, You need to add the above in upload File Method of Home controller in your code.

  1. The code above checks file size and extensions which are not allowed.
  2. So in return i have used your return statements, as if i return badrequest then the code is not working. Thus i returned an ok with empty parameters assuming that the request will be processed and the file loader on chat window stops by this.

Thanks. @rpaschoal .

If Possible kindly provide code updates for Video Button Issue, as i received no reply for shared issue: https://github.com/rpaschoal/ng-chat/issues/116