-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(downloads) add a downloads plugin
It can use the same (or different) files from the web root and will send them as downloads to the browser. See the example updates.
- Loading branch information
Showing
5 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- A plugin that allows to download files via a browser. | ||
local Downloads = {} | ||
Downloads.__index = Downloads | ||
|
||
--- Creates a new plugin instance. | ||
-- The plugin will only respond to `GET` requests. The files will be served from the | ||
-- same `location` setting as defined in the `Handler`. The `prefix` is a virtual folder | ||
-- that triggers the plugin, but will be removed from the filepath if `stripPrefix` is truthy. | ||
-- If `stripPrefix` is falsy, then it should be a real folder. | ||
-- @tparam options table the options table with the following fields; | ||
-- @tparam[opt="downloads/"] options.prefix string the path prefix that triggers the plugin | ||
-- @tparam options.stripPrefix bool whether to strip the prefix from the file path when looking | ||
-- for the file in the filesystem. Defaults to `false`, unless `options.prefix` is omitted, | ||
-- then it defaults to `true`. | ||
function Downloads:new(options) | ||
options = options or {} | ||
self = {} | ||
|
||
if not options.prefix then | ||
self.prefix = "downloads/" | ||
if options.stripPrefix == nil then | ||
self.stripPrefix = true | ||
else | ||
self.stripPrefix = not not options.stripPrefix | ||
end | ||
else | ||
self.prefix = options.prefix | ||
self.stripPrefix = not not options.stripPrefix | ||
end | ||
|
||
self.prefix = "/" .. self.prefix .. "/" | ||
while self.prefix:find("//") do | ||
self.prefix = self.prefix:gsub("//", "/") | ||
end | ||
|
||
setmetatable(self, Downloads) | ||
|
||
return self | ||
end | ||
|
||
function Downloads:newRequestResponse(request, response) | ||
local stop = false | ||
if request:method() ~= "GET" then | ||
return stop -- we only handle GET requests | ||
end | ||
|
||
local path = request:path() | ||
if path:find(self.prefix, nil, true) ~= 1 then | ||
return stop -- doesn't match our prefix | ||
end | ||
|
||
local location = response._writeHandler.location or "" | ||
local filename = path | ||
if self.stripPrefix then | ||
filename = path:sub(#self.prefix + 1, -1) | ||
end | ||
|
||
stop = not response:sendFile('.' .. location .. filename) | ||
return stop | ||
end | ||
|
||
|
||
return Downloads |