Пример #1
0
 public CabinetContextSeed(CabinetContext context, IWebHostEnvironment env, IStorage storage, ILogger <CabinetContextSeed> logger)
 {
     _context = context;
     _env     = env;
     _storage = storage;
     _logger  = logger;
 }
Пример #2
0
        private async Task <IEnumerable <Document> > seedDocuments(CabinetContext _context,
                                                                   IStorage storage,
                                                                   int documentTypeID)
        {
            var files = Directory.GetFiles($"{_env.WebRootPath}/Seed");

            foreach (var file in files)
            {
                var document = new Document
                {
                    ID             = Guid.NewGuid(),
                    OrganizationID = 1,
                    DocumentTypeID = documentTypeID,
                    Name           = Path.GetFileNameWithoutExtension(file)
                };
                var fileInfo            = new FileInfo(file);
                var originalId          = Guid.NewGuid();
                var originalDescription = new OriginalDescription
                {
                    ID            = originalId,
                    DocumentID    = document.ID,
                    Extension     = fileInfo.Extension,
                    FileName      = Path.GetFileNameWithoutExtension(file),
                    ForSign       = true,
                    Size          = fileInfo.Length,
                    StoragePath   = Path.Combine(document.ID.ToString(), $"{originalId}{fileInfo.Extension}"),
                    StorageSource = storage.Source
                };


                await storage.UploadObjectAsync(originalDescription.StoragePath, File.ReadAllBytes(file));

                _context.Documents.Add(document);
                _context.OriginalsDescriptions.Add(originalDescription);
            }

            await _context.SaveChangesAsync();

            return(await _context.Documents.ToListAsync());
        }