public async Task <IActionResult> PostAsync([FromForm] FilePostModel model)
        {
            var result = await fileUpload.GenerateUserAvatarSource(model);

            if (result != null)
            {
                return(Ok(result));
            }

            return(BadRequest("File required"));
        }
Example #2
0
        public async Task <string> GenerateUserAvatarSource(FilePostModel model)
        {
            if (model.File == null)
            {
                return(null);
            }

            var avatarFileExtension = GetAvatarFileFormat(model.File.FileName);

            if (avatarFileExtension == null || model.File.Length == 0)
            {
                return(null);
            }

            var hash = string.Empty;

            using (var md5 = MD5.Create())
            {
                var stream = model.File.OpenReadStream();
                hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant();
            }

            var uploads = Path.Combine(hostingEnvironment.ContentRootPath, AvatarFolder + "\\" + model.UserId);

            if (!Directory.Exists(uploads))
            {
                Directory.CreateDirectory(uploads);
            }

            var avatarPath = Path.Combine(uploads, $"{hash}{avatarFileExtension}");

            if (!System.IO.File.Exists(avatarPath))
            {
                using (var fileStream = new FileStream(avatarPath, FileMode.Create))
                {
                    await model.File.CopyToAsync(fileStream);

                    Resize(avatarPath, uploads, $"{hash}{avatarFileExtension}");
                }

                File.Delete(avatarPath);
            }

            return($"{hash}{avatarFileExtension}");
        }
        public IHttpActionResult Upload(FilePostModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (string.IsNullOrEmpty(model.FileBase64String))
            {
                throw new ArgumentNullException("fileContent");
            }
            if (string.IsNullOrEmpty(model.FileName))
            {
                throw new ArgumentNullException("fileName");
            }
            var fileByte = Convert.FromBase64String(model.FileBase64String);
            var file     = _fileService.SaveFile(model.Group, model.FileName, fileByte, model.ContentType);

            return(this.Ok(file.Id));
        }
Example #4
0
        public void TestAddFileCommand()
        {
            // Arrange
            var model = new FilePostModel
            {
                Name    = FileNameConst,
                FileUri = FileUriConst,
                Format  = FileFormatConst
            };

            var addCommand = new FilesAddCommand(GetFakeApiController(), GetFakeFileRepository(), model);

            // Act
            Task <HttpResponseMessage> addTask = addCommand.Execute();

            addTask.Wait();

            // Assert
            Assert.IsFalse(addTask.IsFaulted);
            Assert.IsNotNull(addTask.Result);
            Assert.AreEqual(addTask.Result.StatusCode, HttpStatusCode.Created);
            Assert.IsInstanceOfType(addTask.Result.Content, typeof(ObjectContent <String>));
            _repositoryMock.Verify(m => m.AddAsync(UserIdConst, FileNameConst, FileUriConst, FileFormatConst), Times.Once());
        }
        public void TestAddFileCommand()
        {
            // Arrange
            var model = new FilePostModel
                {
                    Name = FileNameConst,
                    FileUri = FileUriConst,
                    Format = FileFormatConst
                };

            var addCommand = new FilesAddCommand(GetFakeApiController(), GetFakeFileRepository(), model);

            // Act
            Task<HttpResponseMessage> addTask = addCommand.Execute();
            addTask.Wait();

            // Assert
            Assert.IsFalse(addTask.IsFaulted);
            Assert.IsNotNull(addTask.Result);
            Assert.AreEqual(addTask.Result.StatusCode, HttpStatusCode.Created);
            Assert.IsInstanceOfType(addTask.Result.Content, typeof (ObjectContent<String>));
            _repositoryMock.Verify(m => m.AddAsync(UserIdConst, FileNameConst, FileUriConst, FileFormatConst), Times.Once());
        }