Ejemplo n.º 1
0
        private Task HandleFileHeadRequestAsync(HttpListenerContext context, string filePath)
        {
            if (FilesPreprocessingEnabled && FilePreprocessController.FileInProgress(filePath))
            {
                LogDebug($"File {filePath} was requested but is in progress");
                ResponseFactory.BuildResponse(context, HttpStatusCode.ServiceUnavailable, null);
                return(Task.FromResult(0));
            }

            if (!File.Exists(filePath))
            {
                LogDebug($"File {filePath} was requested but was not found");
                ResponseFactory.BuildResponse(context, HttpStatusCode.NotFound, null);
                return(Task.FromResult(0));
            }

            var response = context.Response;
            var fileInfo = new FileInfo(filePath);

            response.ContentLength64 = fileInfo.Length;
            response.StatusCode      = (int)HttpStatusCode.OK;
            response.ContentType     = MimeTypes.GetTypeByExtenstion(Path.GetExtension(filePath).Substring(1));

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
 public void Stop()
 {
     if (FilesPreprocessingEnabled && FilesEnabled)
     {
         _preprocessTimer?.Stop();
         FilePreprocessController.Stop();
     }
     _started = false;
 }
Ejemplo n.º 3
0
        public async Task <FileOperationStatus> AddFileAsync(string sectionName, string filename, Stream content)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(FileOperationStatus.BadParameters);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

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

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

            if (File.Exists(filePath))
            {
                return(FileOperationStatus.Conflict);
            }

            try
            {
                using (var file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    await content.CopyToAsync(file).ConfigureAwait(false);

                    await file.FlushAsync();
                }
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }

            if (FilesPreprocessingEnabled)
            {
                return(FilePreprocessController.EnqueueFile(filePath)
                    ? FileOperationStatus.JobQueued
                    : FileOperationStatus.Ok);
            }
            return(FileOperationStatus.Ok);
        }
Ejemplo n.º 4
0
 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);
         }
     }
 }
Ejemplo n.º 5
0
        private async Task HandleFileGetRequestAsync(HttpListenerContext context, string filePath)
        {
            if (FilesPreprocessingEnabled && FilePreprocessController.FileInProgress(filePath))
            {
                LogDebug($"File {filePath} was requested but is in progress");
                ResponseFactory.BuildResponse(context, HttpStatusCode.ServiceUnavailable, null);
                return;
            }

            var fileRequest = FileRequestFactory.BuildRequest(filePath, context.Request);

            if (fileRequest == null)
            {
                LogDebug($"Failed to build file request for {filePath}");
                ResponseFactory.BuildResponse(context, HttpStatusCode.BadRequest, null);
                return;
            }
            await fileRequest.BuildResponse(context);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        private async Task HandleFilePostRequestAsync(HttpListenerContext context, FileSection section, string filePath)
        {
            if (File.Exists(filePath))
            {
                LogDebug($"File {filePath} already exists");
                ResponseFactory.BuildResponse(context, HttpStatusCode.Conflict, null);
                return;
            }
            var fileContent   = context.Request.InputStream;
            var contentLength = context.Request.ContentLength64;

            if (section.MaxFileSize > 0 && contentLength > section.MaxFileSize)
            {
                LogDebug($"Trying to create file of size {contentLength} in section {section.Name} with max size of {section.MaxFileSize}");
                ResponseFactory.BuildResponse(context, HttpStatusCode.RequestEntityTooLarge, null);
                return;
            }
            using (var file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                await fileContent.CopyToAsync(file).ConfigureAwait(false);

                await file.FlushAsync();
            }
            GC.Collect(1);
            LogTrace($"Total memory: {GC.GetTotalMemory(true)}");
            LogDebug($"File {filePath} created");
            if (FilesPreprocessingEnabled)
            {
                var code = FilePreprocessController.EnqueueFile(filePath) ? HttpStatusCode.Accepted : HttpStatusCode.Created;
                ResponseFactory.BuildResponse(context, code, null);
            }
            else
            {
                ResponseFactory.BuildResponse(context, HttpStatusCode.Created, null);
            }
        }