Exemplo n.º 1
0
        public async Task <FileResult> Get(string fileName, string token = null, string password = null)
        {
            if (string.IsNullOrWhiteSpace(token) && string.IsNullOrWhiteSpace(password))
            {
                User user = await _unitOfWork.UserManager.GetUserAsync(User);

                if (user != null)
                {
                    token    = user.Token;
                    password = _decryptService.DecryptData(user.Password);
                }
                else
                {
                    return(null);
                }
            }

            //Checking data for validation
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                //Result of getting data from the databse
                ResultOfApiRequest <UserFile> result = await _getDataService.GetFile(token, password, fileName);

                //Cheking if length of bytes of the file more than 0
                if (result.Data.Data.Length > 0)
                {
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        //Creating the file for uploading to user
                        using (FileStream fs = new FileStream(result.Data.FileName, FileMode.Create))
                        {
                            await fs.WriteAsync(result.Data.Data, 0, result.Data.Data.Length);
                        }

                        //Creating file info that to get extension of the file
                        FileInfo fileInfo  = new FileInfo($"DataFromUsers/{fileName}");
                        string   extension = fileInfo?.Extension.Trim(new char[] { '.' });

                        return(PhysicalFile(fileInfo.FullName, $"application/{extension}", fileName));
                    }
                }
                else
                {
                    return(null);
                }
            }

            return(null);
        }