-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlite_app
executable file
·163 lines (150 loc) · 5.81 KB
/
lite_app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::ByteStream qw(b);
use Mojo::File qw(path tempfile);
use Mojo::JWT;
use Mojo::SQLite;
use Mojo::SMTP::Client;
use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
use Archive::Unzip::Burst qw(unzip);
plugin 'Config' => {default => {
session_expiration => 31_536_000,
jwt_timeout => 86_400,
secrets => [app->moniker],
sqlite => "$0.db",
}};
app->secrets(app->config('secrets'));
hook before_dispatch => sub {
my $c = shift;
$c->session(expiration => app->config('session_expiration'));
$c->app->static->paths([$c->app->home->child('public', $c->domain), $c->app->home->child('public', 'dropzone')]);
return if $c->req->url->path->parts->[0] && $c->req->url->path->parts->[0] eq 'dropzone';
#my $default_index = path($c->app->static->paths->[0])->list->map('basename')->grep(qr/^index.html?/)->first;
my $redir = path($c->app->static->paths->[0])->sibling($c->domain.'.301');
if ( -e "$redir" ) {
$redir = b($redir->slurp)->split("\n")->each(sub{$_=[split /\t/, $_]})->grep(sub{$_->[0] eq $c->req->url->path->trailing_slash(0)});
return $c->redirect_to($redir->first->[1]) if $redir->size;
}
my $static = path($c->app->static->paths->[0])->child(@{$c->req->url->path->parts})->tap(sub{-d and $_=$_->child($_->list->map('basename')->grep(qr/^index.html?/)->first)})->to_rel($c->app->static->paths->[0]);
$c->reply->static($static);
};
hook after_static => sub {
my $c = shift;
$c->res->headers->content_type('text/html;charset=UTF-8') if $c->res->headers->content_type =~ m!text/plain!;
};
helper domain => sub { ((split /:/, shift->req->headers->host)[0]) =~ s/www\.//r };
helper smtp => sub { state $smtp = Mojo::SMTP::Client->new(address => app->config('sendgrid')->{host} || 'smtp.sendgrid.net', autodie => 0) };
helper sqlite => sub { state $sqlite = Mojo::SQLite->new(app->config('sqlite')) };
helper jwt => sub { Mojo::JWT->new(expires => time + app->config('jwt_timeout'), secret => app->secrets->[0]) };
app->sqlite->migrations->from_data->migrate;
under '/dropzone' => sub {
my $c = shift;
return 0 unless $c->sqlite;
return 0 unless -d $c->app->home->child('public', $c->domain);
my $site = $c->sqlite->db->select('sites', ['email'], {domain => $c->domain})->hash;
$c->stash(email => $site ? $site->{email} : '');
return 1 if $c->session($c->domain);
if ( $c->param('jwt') ) {
if ( my $claims = $c->jwt->decode($c->param('jwt')) ) {
return 0 unless $claims->{domain} eq $c->domain;
$c->session($claims->{domain} => 1);
return 1;
}
}
return 1 unless $site;
my $jwt = $c->jwt->claims({domain => $c->domain})->encode;
# TODO: Use a template
my $from = sprintf('webhosting@%s', $c->domain);
my $to = $site->{email};
my $subj = sprintf('Webhosting for %s', $c->domain);
my $resp = $c->smtp->send(
auth => {type => 'login', login => 'apikey', password => $c->config('sendgrid')->{apikey}},
from => $from,
to => $to,
data => join("\r\n", 'MIME-Version: 1.0',
"From: $from",
"To: $to",
"Subject: $subj",
'Content-Type: text/html; charset=UTF-8',
'',
sprintf('<a href="%s">Login</a>', $c->url_for('dropzone')->query(jwt => $jwt)->to_abs),
),
quit => 1,
);
if ( !ref $resp ) {
warn "fail"
} elsif ( $resp->error ) {
warn $resp->error;
} else {
$c->render(text => "email sent to $site->{email}");
}
#$c->sendgrid->mail(
# to => $site->{email},
# from => sprintf('webhosting@%s', $c->domain),
# subject => sprintf('Webhosting for %s', $c->domain),
# html => sprintf('<a href="%s">Login</a>', $c->url_for('dropzone')->query(jwt => $jwt)->to_abs)
#)->send;
#$c->render(text => "email sent to $site->{email}");
return 0;
};
get '/' => 'dropzone';
get '/download' => sub {
my $c = shift;
my $zip = Archive::Zip->new();
my $dir = $c->app->home->child('public', $c->domain);
$zip->addTree($dir, '', sub { -f && -r } );
my $zipfile = tempfile(DIR => $dir);
return $c->reply->not_found unless $zip->writeToFileNamed("$zipfile") == AZ_OK;
$c->log->info("Downloaded $dir as $zipfile");
$c->res->headers->content_type('application/zip');
$c->res->headers->content_disposition(sprintf 'attachment; filename="%s.zip"', $c->domain);
$c->reply->static($zipfile->basename);
unlink $zipfile;
};
post '/upload' => sub {
my $c = shift;
if ( $c->param('email') ) {
$c->sqlite->db->delete('sites', {domain => $c->domain});
$c->sqlite->db->insert('sites', {domain => $c->domain, email => $c->param('email')})
}
my $domain = $c->req->headers->host;
my $upload = $c->req->upload('file');
return $c->reply->not_found unless $upload->headers->content_type eq 'application/zip' || $upload->headers->content_type eq 'application/x-zip-compressed';
my $tmp = tempfile;
$upload->move_to($tmp);
my $dest = $c->app->static->paths->[0];
unzip($tmp, $dest);
$c->log->info(sprintf "Uploaded %s to $dest", $upload->filename);
unlink $tmp;
$c->render(json => {ok => 1});
};
app->start;
__DATA__
@@ migrations
-- 1 up
create table sites (domain varchar(255) unique not null, email varchar(255) not null);
-- 1 down
drop table if exists sites;
@@ dropzone.html.ep
<html>
<head>
<link rel="stylesheet" href="/dropzone.css">
<link rel="stylesheet" href="/style.css">
<script src="/dropzone.js"></script>
</head>
<body>
Domain: <%= link_to $c->req->headers->host => '/' %><br />
%= link_to "Download Website" => 'download'
<div id="dropzone">
<form action="/dropzone/upload" class="dropzone needsclick dz-clickable" id="demo-upload">
<div>
%= label_for email => 'Email'
%= email_field 'email' => $email
<div class="dz-message needsclick">
Drop files here or click to upload.<br>
</div>
</div>
</form>
</div>
</body>
</html>