Ejemplo n.º 1
0
        public async Task <IActionResult> InsertAsync([FromQuery] string name, [FromQuery] FileType fileType)
        {
            var author = await _fileEntityService.GetOrCreateUserAsync(User.Identity.Name);

            var id   = Guid.NewGuid();
            var path = $"{DateTime.Today:yyyy-MM-dd}\\{id}";

            var fileReference = new FileReference {
                Id = id, Author = author, Name = name, FileType = fileType, Path = path
            };

            try
            {
                await _fileEntityService.InsertFileReferenceAsync(fileReference);
            }
            catch (FileReferenceAlreadyExistsException)
            {
                return(StatusCode(409, $"A file with name {name} already exists."));
            }

            using (var ms = new MemoryStream())
            {
                await Request.Body.CopyToAsync(ms);

                await _fileSystemService.WriteFileAsync(path, ms.ToArray());
            }

            return(Json(fileReference));
        }
Ejemplo n.º 2
0
        public async Task <FileReference> InsertAsync([FromUri] string name, [FromUri] FileType fileType)
        {
            var author = await _fileEntityService.GetOrCreateUserAsync(User.Identity.Name);

            var id   = Guid.NewGuid();
            var path = $"{DateTime.Today:yyyy-MM-dd}\\{id}";

            var fileReference = new FileReference {
                Id = id, Author = author, Name = name, FileType = fileType, Path = path
            };

            try
            {
                await _fileEntityService.InsertFileReferenceAsync(fileReference);
            }
            catch (FileReferenceAlreadyExistsException)
            {
                throw new HttpResponseException(
                          new HttpResponseMessage(System.Net.HttpStatusCode.Conflict)
                {
                    Content = new StringContent($"A file with name {name} already exists.")
                });
            }

            using (var ms = new MemoryStream())
            {
                using (var requestStream = await Request.Content.ReadAsStreamAsync())
                {
                    await requestStream.CopyToAsync(ms);

                    await _fileSystemService.WriteFileAsync(path, ms.ToArray());
                }
            }

            return(fileReference);
        }