Added MIME types library for static file serving

This commit is contained in:
Yessiest 2023-09-20 22:57:21 +04:00
parent 56e3907250
commit 960dbf92ea
2 changed files with 1285 additions and 2 deletions

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "../probe" require_relative "../probe"
require_relative "../util/mime"
module Landline module Landline
module Handlers module Handlers
@ -18,12 +19,18 @@ module Landline
# Method callback on successful request navigation. # Method callback on successful request navigation.
# Tries to serve files matched by handler # Tries to serve files matched by handler
# @param request [Landline::Request] # @param request [Landline::Request]
# @return [Boolean] true if file was found # @return [Boolean, Array] true if file was found
def process(request) def process(request)
path = File.expand_path(request.filepath) path = File.expand_path(request.filepath)
return unless path.start_with? @properties["path"] return unless path.start_with? @properties["path"]
File.open(path.delete_suffix("/")) filepath = path.delete_suffix("/")
[200,
{
"content-type" => Landline::MIME.get_mime_type(filepath)
},
File.open(filepath)]
rescue StandardError rescue StandardError
false false
end end

1276
lib/landline/util/mime.rb Normal file

File diff suppressed because it is too large Load Diff