Beispiel #1
0
        public async Task <IActionResult> PutFile(Guid id, File file)
        {
            if (id != file.Id)
            {
                return(BadRequest());
            }

            context.Entry(file).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <File> > PostFile([FromForm] FileModel fileModel)
        {
            if (fileModel == null)
            {
                return(BadRequest());
            }

            var claims = HttpContext.User.Claims;
            var email  = claims.Where(c => c.Type == ClaimTypes.Name).FirstOrDefault().Value;
            var user   = await context.AppUsers.FirstOrDefaultAsync(u => u.Email == email);

            string clientFilesFolder = configuration.GetValue <string>("ClientFilesFolderPath");

            string userFolder = Path.Combine(clientFilesFolder, user.Id.ToString());

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

            // to be sure file with the same Guid does not exist already
            Guid   fileGuid;
            string filePath;
            var    ime = fileModel.Content.FileName;

            do
            {
                fileGuid = Guid.NewGuid();
                filePath = Path.Combine(userFolder, fileGuid.ToString());
            } while (System.IO.File.Exists(filePath));


            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await fileModel.Content.CopyToAsync(stream);
            }
            //await System.IO.File.WriteAllBytesAsync(filePath, fileModel.Content.);

            byte[] hashValueBytes = Convert.FromBase64String(fileModel.HashValue);
            File   fileToAdd      = new File
            {
                Id        = fileGuid,
                FileName  = fileModel.FileName,
                Path      = filePath,
                HashValue = hashValueBytes,
                Created   = DateTime.UtcNow,
                UserId    = user.Id
            };

            await context.Files.AddAsync(fileToAdd);

            await context.SaveChangesAsync();

            return(CreatedAtAction("GetFile", new { id = fileToAdd.Id }, fileToAdd));
        }