first semi-functional file browser
[cloudstore.git] / web-api.pl
1 #!/usr/bin/env perl
2
3 use Mojolicious::Lite;
4 use Data::Dump qw(dump);
5
6 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
7 plugin 'pod_renderer';
8
9 helper get_files => sub {
10         my $self = shift;
11 warn "XXXXX ", dump( $self->req->url->path->to_string );
12
13         my $path = $self->req->url->path->to_string;
14         $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; # url_unescape;
15         $path =~ s{^.*~/}{};
16
17         $self->stash( full_path => $path );
18
19         return $self->redirect_to( $path ) if -f $path;
20
21         my $files = [
22                 map {
23                         $_ .= '/' if -d $_;
24                         s{$path/}{};
25                         $_;
26                 }
27                 glob "$path/*"
28         ];
29
30         warn "# path $path ",dump($files);
31
32         $self->stash( files => $files );
33 };
34
35 get '/~/*path' => sub {
36         my $self = shift;
37         warn "no path";
38         $self->get_files;
39
40         if ( -f $self->stash('full_path') ) {
41                 return $self->redirect_to( $self->stash( 'full_path' ) );
42         }
43
44         $self->render('user');
45 };
46
47 get '/' => sub {
48         my $self = shift;
49         $self->render('index');
50 };
51
52 get '/_admin/users' => sub {
53         my $self = shift;
54
55         my @users =
56                 map { s{/secrets}{}; $_ }
57                 glob   'users/*/secrets';
58
59         $self->render('users', users => \@users );
60 };
61
62 app->start;
63 __DATA__
64
65 @@ layouts/default.html.ep
66 <!doctype html><html>
67   <head><title><%= title %></title></head>
68   <body>
69 <%= content %>
70   </body>
71 </html>
72
73
74 @@ index.html.ep
75 % layout 'default';
76
77 <%= link_to 'list users' => '_adminusers' %>
78
79
80 @@ users.html.ep
81 % layout 'default';
82 % title 'Welcome to CloudShare';
83
84 <h1>Users</h1>
85 <ul>
86 % foreach my $login ( @$users ) {
87 <li><a href="/~/<%= $login %>/"><%= $login %></a>
88 % }
89 </ul>
90
91
92 @@ user.html.ep
93 % layout 'default';
94
95 <h1><%= $full_path %></h1>
96
97 <ul>
98 % foreach my $path ( @$files ) {
99 <li><a href="<%= $path %>"><%= $path %></a>
100 % }
101 </ul>
102