Beispiel #1
0
        private readonly VideoRepositoryOptions _videoRepositoryOptions;            //Video repostiroy settings

        /// <summary>
        /// Caching system for streaming video files.
        /// </summary>
        /// <param name="fso">FileStoreOptions</param>
        /// <param name="vro">VideoRepositoryOptions</param>
        public StreamCache(FileStoreOptions fso, VideoRepositoryOptions vro)
        {
            _fileStoreOptions       = fso;
            _videoRepositoryOptions = vro;
            _cachedVideos           = new ConcurrentDictionary <Guid, byte[]>();
            _cacheTimeout           = new ConcurrentDictionary <Guid, Timer>();
        }
Beispiel #2
0
    protected override ValidationResult?IsValid(object?value, ValidationContext validationContext)
    {
        if (!(value is IFormFile file))
        {
            return(new ValidationResult("File is required"));
        }

        FileStoreOptions options = validationContext.GetRequiredService <IOptions <FileStoreOptions> >().Value;

        long maxFileSizeInBytes = (long)options.MaximumFileSize.Megabytes().Bytes;

        if (file.Length > maxFileSizeInBytes)
        {
            string actualSize  = file.Length.Bytes().Humanize("MB");
            string maximumSize = options.MaximumFileSize.Megabytes().ToString("MB");

            return(new ValidationResult($"The file is too large, it is {actualSize} but the maximum allowed size {maximumSize}"));
        }

        return(ValidationResult.Success);
    }
Beispiel #3
0
 public StreamContent(ApplicationDbContext dbContext)
 {
     _dbContext = dbContext;
     _fileStore = OptionsFactory.GetFileStoreOptions();
 }
Beispiel #4
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.
            }
        }
Beispiel #5
0
 public FileStoreFileProviderFactory(IWebHostEnvironment hostingEnvironment, IOptions <FileStoreOptions> fileStoreOptions)
 {
     this._hostingEnvironment = hostingEnvironment;
     this._fileStoreOptions   = fileStoreOptions.Value;
 }