private void PreprocessExistingFiles()
 {
     foreach (var section in FileSections)
     {
         var directory = Path.Combine(FilesLocation, section.Folder);
         foreach (var file in Directory.GetFiles(directory).Where(f => !f.Contains(Constants.ChangedString)))
         {
             if (IoHelper.LoadAllChanged(file).Any())
             {
                 continue;
             }
             FilePreprocessController.EnqueueFile(file);
         }
     }
 }
        public bool Start(FileRequestControllerConfiguration <TAccount> configuration, ServerStatistics <TAccount> statistics)
        {
            FilesEnabled                        = configuration.FilesEnabled;
            FilesBaseUri                        = configuration.FilesBaseUri;
            FilesLocation                       = configuration.FilesLocation;
            FaviconPath                         = configuration.FaviconPath;
            FilesNeedAuthorization              = configuration.FilesNeedAuthorization;
            FileSections                        = configuration.FileSections;
            FilesPreprocessingEnabled           = configuration.FilesPreprocessingEnabled;
            ExistingFilesPreprocessingFrequency = configuration.ExistingFilesPreprocessingFrequency;
            _filesAuthorizer                    = configuration.FilesAuthorizer;
            _statistics                         = statistics;

            if (!IoHelper.HasWriteAccessToDirectory(FilesLocation))
            {
                LogMessage(LogLevel.Warning, $"Cannot use file folder {FilesLocation} : no write access");
                return(false);
            }

            if (!InitializeFileSections())
            {
                LogMessage(LogLevel.Warning, "Failed to initialize file sections");
                return(false);
            }

            if (FilesPreprocessingEnabled && FilesEnabled)
            {
                FilePreprocessController.Start(configuration.FilesPreprocessorThreads,
                                               configuration.FilesCompressionEnabled);
                if (ExistingFilesPreprocessingFrequency > 0)
                {
                    PreprocessExistingFiles();
                    _preprocessTimer          = new Timer(1000 * ExistingFilesPreprocessingFrequency);
                    _preprocessTimer.Elapsed += (sender, args) => PreprocessExistingFiles();
                    _preprocessTimer.Start();
                }
            }

            _started = true;
            return(true);
        }
        private void HandleFileDeleteRequest(HttpListenerContext context, string filePath)
        {
            if (!File.Exists(filePath))
            {
                LogDebug($"File {filePath} does not exist and cannot be deleted");
                ResponseFactory.BuildResponse(context, HttpStatusCode.NotFound, null);
                return;
            }

            var compressed = IoHelper.LoadAllChanged(filePath);

            try
            {
                File.Delete(filePath);
                compressed.ForEach(File.Delete);
                ResponseFactory.BuildResponse(context, HttpStatusCode.OK, null);
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                ResponseFactory.BuildResponse(context, HttpStatusCode.InternalServerError, null);
            }
        }
        public FileOperationStatus DeleteFile(string sectionName, string filename)
        {
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(FileOperationStatus.NotFound);
            }

            if (filename.Contains("_CHANGED_"))
            {
                return(FileOperationStatus.BadParameters);
            }
            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            if (!File.Exists(filePath))
            {
                LogDebug($"File {filePath} does not exist and cannot be deleted");
                return(FileOperationStatus.NotFound);
            }

            var compressed = IoHelper.LoadAllChanged(filePath);

            try
            {
                File.Delete(filePath);
                compressed.ForEach(File.Delete);
                return(FileOperationStatus.Ok);
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }
        }
Example #5
0
        public static FileType GetFileTypeByName(string fileName)
        {
            var extension = IoHelper.GetExtension(fileName);

            return(GetFileTypeByExtension(extension));
        }