Example #1
0
        public async Task <ActionResult> CreateFile(FileForCreationDto fileForCreationDto)
        {
            var path = String.Concat(FilePath, fileForCreationDto.FileName);


            var logFile   = System.IO.File.Create(path);
            var logWriter = new System.IO.StreamWriter(logFile);

            logWriter.WriteLine($"Your file was created at: {path}");
            logWriter.WriteLine($"Your text: {fileForCreationDto.Text}");

            logWriter.Dispose();

            using (var httpClient = new HttpClient())
            {
                var parameters     = new Dictionary <string, string>();
                var encodedContent = new FormUrlEncodedContent(parameters);

                try
                {
                    var response = await httpClient.PostAsync("https://localhost:44374/api/aws", encodedContent);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(Problem("Your file wasn't able to be uploaded"));
                    }

                    return(Ok("Your file was succesfully created and uploaded to AWS!"));
                }
                catch (Exception e)
                {
                    return(Problem($"There was an error {e.Message}", $"{e.InnerException}"));
                }
            };
        }
Example #2
0
        public async Task <IActionResult> SaveFiles(int userId,
                                                    [FromForm] FileForCreationDto fileForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }


            var file         = fileForCreationDto.File;
            var uploadResult = new RawUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new RawUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                };
            }
            fileForCreationDto.PublicId = uploadResult.PublicId;
            fileForCreationDto.Url      = uploadResult.Url.ToString();

            var fileToSave = _mapper.Map <File>(fileForCreationDto);
            await _repo.AddFiles(fileForCreationDto.CourseCode, fileToSave);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Couldn't save the file"));
        }
Example #3
0
        public async Task <IActionResult> CreateAndUploadFile(FileForCreationDto fileForCreationDto)
        {
            var fileValues = await _fileService.CreateFile(_mapper.Map <FileValues>(fileForCreationDto));

            var error = await _fileCloudProviderService.UploadFile(fileValues);

            if (error.HasError)
            {
                return(Problem(error.Message));
            }

            return(Ok("Your file was created and uploaded to AWS!"));
        }
Example #4
0
        public async Task <IActionResult> AddFileForUser(int userId,
                                                         [FromForm] FileForCreationDto fileForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

            var file = fileForCreationDto.File;

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream()){
                    string imageName = System.IO.Path.GetFileName(file.FileName);
                }
            }

            return(BadRequest("Could not add the file"));
        }