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