public static DalLotImage ToDalLotImage(this LotImage lotImage)
 {
     return(new DalLotImage()
     {
         Id = lotImage.ImageId,
         Content = lotImage.Content,
         LotRefId = lotImage.LotRefId
     });
 }
Beispiel #2
0
        public async Task <IActionResult> UploadImageLot([FromForm] List <IFormFile> files, int id)
        {
            long size = files.Sum(f => f.Length);

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    var path = Path.GetTempFileName();

                    LotImage lastImage = null;

                    if (_context.LotImage.Count() > 0)
                    {
                        lastImage = await _context.LotImage
                                    .OrderBy(x => x.ID)
                                    .LastAsync();
                    }

                    int lastImageID = 1;
                    if (lastImage != null)
                    {
                        lastImageID = (lastImage.ID + 1);
                    }

                    using (var stream = System.IO.File.Create(path))
                    {
                        await file.CopyToAsync(stream);
                    }

                    var newpath = VeilingConfiguration.LotImageLocation + $"/{id}/{lastImageID}{FileContentType.GetExtension(file.ContentType)}";
                    Directory.CreateDirectory(Path.GetDirectoryName(newpath));
                    System.IO.File.Delete(newpath);

                    _context.LotImage.Add(new LotImage()
                    {
                        LotID = id, ImageLocation = newpath, ID = lastImageID, AspectRatio = LotImage.CalculateAspectRatio(file.OpenReadStream())
                    });
                    await _context.SaveChangesAsync();

                    System.IO.File.Move(path, newpath);
                    System.IO.File.Delete(path);
                }
            }

            return(Ok(new { count = files.Count, size }));
        }