Пример #1
0
 public StreamContent(ApplicationDbContext dbContext)
 {
     _dbContext = dbContext;
     _fileStore = OptionsFactory.GetFileStoreOptions();
 }
Пример #2
0
        public static void Main(string[] args)
        {
            _connectionString = OptionsFactory.GetConnectionString();                   //Get connection string options
            _fileStoreOptions = OptionsFactory.GetFileStoreOptions();                   //Get file store options
            _dbContext        = new ApplicationDbContext(                               //Create database context
                new DbContextOptionsBuilder <ApplicationDbContext>()
                .UseSqlServer(_connectionString.DefaultConnection).Options);

            //Fill default picture data
            using (FileStream fs = new FileStream("NoPicture.png", FileMode.Open, FileAccess.Read))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    _defaultImagedata = ms.ToArray();
                }
            }

            bool error = false;                                                         //If an error was thrown.

            InitializeFileSystem();                                                     //Make folders if they don't exist.
            var filenames = Directory.GetFiles(_fileStoreOptions.DumpPath);

            if (filenames.Length == 0)
            {
                System.Diagnostics.Debug.WriteLine("No new files to add.");
                return;
            }
            foreach (var fp in filenames)          //Create DB entries and copy files to correct locations.
            {
                try
                {
                    CopyFileToMediaLibrary(fp);
                }
                catch (UnsupportedFileException ex)                                     //If unsupported type move to rejected path.
                {
                    var adminMessage = CreateAdminMessage(string.Format("Failed to add {0} to database. File type {1} incompatible.", ex.FileName, ex.MediaType), ErrorStatus.error);
                    _dbContext.AdminMessages.Add(adminMessage);
                    //Rejected path
                    string rejectpath = Path.Combine(_fileStoreOptions.RejectedPath, Path.GetFileName(fp));
                    //Move rejected file
                    File.Move(fp, rejectpath);
                    error = true;
                    continue;
                }
                catch (Exception ex)                                                     //Halt program if other error.
                {
                    var adminMessage = CreateAdminMessage(string.Format("ContentScanner failed with the following error: {0}", ex.Message), ErrorStatus.error);
                    _dbContext.AdminMessages.Add(adminMessage);
                    error = true;
                    throw ex;
                }
            }
            if (error)
            {
                System.Diagnostics.Debug.WriteLine("Process ended with errors.");       //Report program ended with errors.
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Process ended successfully.");      //Report program ended without errors.
            }
        }