-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
401 additions
and
325 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
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,103 @@ | ||
require "./router" | ||
|
||
class AdminRouter < Router | ||
def setup | ||
get "/admin" do |env| | ||
layout "admin" | ||
end | ||
|
||
get "/admin/user" do |env| | ||
users = @context.storage.list_users | ||
username = get_username env | ||
layout "user" | ||
end | ||
|
||
get "/admin/user/edit" do |env| | ||
username = env.params.query["username"]? | ||
admin = env.params.query["admin"]? | ||
if admin | ||
admin = admin == "true" | ||
end | ||
error = env.params.query["error"]? | ||
current_user = get_username env | ||
new_user = username.nil? && admin.nil? | ||
layout "user-edit" | ||
end | ||
|
||
post "/admin/user/edit" do |env| | ||
# creating new user | ||
begin | ||
username = env.params.body["username"] | ||
password = env.params.body["password"] | ||
# if `admin` is unchecked, the body hash | ||
# would not contain `admin` | ||
admin = !env.params.body["admin"]?.nil? | ||
|
||
if username.size < 3 | ||
raise "Username should contain at least 3 characters" | ||
end | ||
if (username =~ /^[A-Za-z0-9_]+$/).nil? | ||
raise "Username should contain alphanumeric characters "\ | ||
"and underscores only" | ||
end | ||
if password.size < 6 | ||
raise "Password should contain at least 6 characters" | ||
end | ||
if (password =~ /^[[:ascii:]]+$/).nil? | ||
raise "password should contain ASCII characters only" | ||
end | ||
|
||
@context.storage.new_user username, password, admin | ||
|
||
env.redirect "/admin/user" | ||
rescue e | ||
@context.error e | ||
redirect_url = URI.new \ | ||
path: "/admin/user/edit",\ | ||
query: hash_to_query({"error" => e.message}) | ||
env.redirect redirect_url.to_s | ||
end | ||
end | ||
|
||
post "/admin/user/edit/:original_username" do |env| | ||
# editing existing user | ||
begin | ||
username = env.params.body["username"] | ||
password = env.params.body["password"] | ||
# if `admin` is unchecked, the body | ||
# hash would not contain `admin` | ||
admin = !env.params.body["admin"]?.nil? | ||
original_username = env.params.url["original_username"] | ||
|
||
if username.size < 3 | ||
raise "Username should contain at least 3 characters" | ||
end | ||
if (username =~ /^[A-Za-z0-9_]+$/).nil? | ||
raise "Username should contain alphanumeric characters "\ | ||
"and underscores only" | ||
end | ||
|
||
if password.size != 0 | ||
if password.size < 6 | ||
raise "Password should contain at least 6 characters" | ||
end | ||
if (password =~ /^[[:ascii:]]+$/).nil? | ||
raise "password should contain ASCII characters only" | ||
end | ||
end | ||
|
||
@context.storage.update_user \ | ||
original_username, username, password, admin | ||
|
||
env.redirect "/admin/user" | ||
rescue e | ||
@context.error e | ||
redirect_url = URI.new \ | ||
path: "/admin/user/edit",\ | ||
query: hash_to_query({"username" => original_username, \ | ||
"admin" => admin, "error" => e.message}) | ||
env.redirect redirect_url.to_s | ||
end | ||
end | ||
end | ||
end |
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,92 @@ | ||
require "./router" | ||
|
||
class APIRouter < Router | ||
def setup | ||
get "/api/page/:tid/:eid/:page" do |env| | ||
begin | ||
tid = env.params.url["tid"] | ||
eid = env.params.url["eid"] | ||
page = env.params.url["page"].to_i | ||
|
||
title = @context.library.get_title tid | ||
raise "Title ID `#{tid}` not found" if title.nil? | ||
entry = title.get_entry eid | ||
raise "Entry ID `#{eid}` of `#{title.title}` not found" if \ | ||
entry.nil? | ||
img = entry.read_page page | ||
raise "Failed to load page #{page} of " \ | ||
"`#{title.title}/#{entry.title}`" if img.nil? | ||
|
||
send_img env, img | ||
rescue e | ||
@context.error e | ||
env.response.status_code = 500 | ||
e.message | ||
end | ||
end | ||
|
||
get "/api/book/:title" do |env| | ||
begin | ||
tid = env.params.url["tid"] | ||
title = @context.library.get_title tid | ||
raise "Title ID `#{tid}` not found" if title.nil? | ||
|
||
send_json env, title.to_json | ||
rescue e | ||
@context.error e | ||
env.response.status_code = 500 | ||
e.message | ||
end | ||
end | ||
|
||
get "/api/book" do |env| | ||
send_json env, @context.library.to_json | ||
end | ||
|
||
post "/api/admin/scan" do |env| | ||
start = Time.utc | ||
@context.library.scan | ||
ms = (Time.utc - start).total_milliseconds | ||
send_json env, { | ||
"milliseconds" => ms, | ||
"titles" => @context.library.titles.size | ||
}.to_json | ||
end | ||
|
||
post "/api/admin/user/delete/:username" do |env| | ||
begin | ||
username = env.params.url["username"] | ||
@context.storage.delete_user username | ||
rescue e | ||
@context.error e | ||
send_json env, { | ||
"success" => false, | ||
"error" => e.message | ||
}.to_json | ||
else | ||
send_json env, {"success" => true}.to_json | ||
end | ||
end | ||
|
||
post "/api/progress/:title/:entry/:page" do |env| | ||
begin | ||
username = get_username env | ||
title = (@context.library.get_title env.params.url["title"]) | ||
.not_nil! | ||
entry = (title.get_entry env.params.url["entry"]).not_nil! | ||
page = env.params.url["page"].to_i | ||
|
||
raise "incorrect page value" if page < 0 || page > entry.pages | ||
title.save_progress username, entry.title, page | ||
rescue e | ||
@context.error e | ||
send_json env, { | ||
"success" => false, | ||
"error" => e.message | ||
}.to_json | ||
else | ||
send_json env, {"success" => true}.to_json | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.