Ejemplo n.º 1
0
        public IActionResult UploadFiles([FromForm] UploadFilesModel model)
        {
            string rootPath = Path.Combine(_environment.ContentRootPath, "Uploads");

            try {
                var destFolder = _db.CajFolder.FirstOrDefault(x => x.Id == model.FolderId);
                if (!System.IO.Directory.Exists(rootPath))
                {
                    Directory.CreateDirectory(rootPath);
                }

                foreach (var file in model.Files)
                {
                    var cajFile = new CajFile()
                    {
                        Created = DateTime.Now,
                        Id      = Guid.NewGuid(),
                        Name    = file.FileName,
                    };
                    _db.CajFile.Add(cajFile);
                    _db.CajFileInFolder.Add(new CajFileInFolder()
                    {
                        FkFile   = cajFile.Id,
                        FkFolder = destFolder.Id,
                        Id       = Guid.NewGuid()
                    });

                    string serverFilePath     = Path.Combine(rootPath, cajFile.Id.ToString());
                    string serverTempFilePath = $"{serverFilePath}.tmp";

                    //copy temp to file
                    using (var fileStream = new FileStream(serverTempFilePath, FileMode.Create)) {
                        var fileMemoryStream = file.OpenReadStream();
                        fileMemoryStream.CopyTo(fileStream);
                    }

                    if (IOManager.Compress(serverTempFilePath, serverFilePath))
                    {
                        _db.SaveChanges();
                        IOManager.RemoveFile(serverTempFilePath);
                    }
                }
            } catch (Exception ex) {
                _logger.Log(LogLevel.Information, $"Error uploading files: {ex}");
                return(StatusCode(500));
            }

            return(StatusCode(200));
        }
Ejemplo n.º 2
0
 public ObjectsModel(CajFile f)
 {
     Id   = f.Id;
     Name = f.Name;
     Type = 0;
 }