1 |
wakaba |
1.1 |
#!/usr/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
use warnings; |
4 |
|
|
|
5 |
|
|
my $param = {}; |
6 |
|
|
for (map {[split /=/, $_, 2]} split /[&;]/, $ENV{QUERY_STRING} || '') { |
7 |
|
|
$param->{$_->[0]} = $_->[1]; |
8 |
|
|
} |
9 |
|
|
|
10 |
|
|
$param->{mode} ||= ''; |
11 |
|
|
die unless $param->{date} =~ /\A[0-9]+\z/ or {list => 1, prev => 1, next => 1}->{$param->{mode}}; |
12 |
|
|
|
13 |
|
|
my $dir_name = './canvas-data'; |
14 |
|
|
my $data_file_name = sprintf '%s/data-%s.txt', $dir_name, $param->{date}; |
15 |
|
|
my $data_file_name_pattern = qr[^data-([0-9]+)\.txt$]; |
16 |
|
|
|
17 |
|
|
sub get_dates () { |
18 |
|
|
my @date; |
19 |
|
|
opendir my $dir, $dir_name or die "$0: $dir_name: $!"; |
20 |
|
|
for (readdir $dir) { |
21 |
|
|
if (/$data_file_name_pattern/) { |
22 |
|
|
push @date, $1; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
return sort {$a <=> $b} @date; |
26 |
|
|
} |
27 |
|
|
|
28 |
wakaba |
1.2 |
if ($param->{mode} eq '' and $ENV{REQUEST_METHOD} eq 'POST') { |
29 |
wakaba |
1.1 |
die if $ENV{CONTENT_LENGTH} > 100_000; |
30 |
|
|
|
31 |
|
|
open my $data_file, '>>', $data_file_name or die "$0: $data_file_name: $!"; |
32 |
|
|
print $data_file 'time,', scalar time, "\x0A"; |
33 |
|
|
read STDIN, my $data, $ENV{CONTENT_LENGTH}; |
34 |
|
|
for (split /;/, $data) { |
35 |
|
|
print $data_file $_, "\x0A"; |
36 |
|
|
} |
37 |
|
|
print "Status: 204 No Content\n\n"; |
38 |
|
|
} elsif ($param->{mode} eq 'list') { |
39 |
|
|
print "Content-Type: text/html\n\n"; |
40 |
wakaba |
1.2 |
print q[<!DOCTYPE HTML><html lang=en><title>List</title> |
41 |
|
|
<meta name="viewport" content="width=device-width"> |
42 |
|
|
<style> |
43 |
|
|
img { |
44 |
|
|
width: 100px; |
45 |
|
|
float: left; |
46 |
|
|
} |
47 |
|
|
ul { |
48 |
|
|
margin: 0; |
49 |
|
|
padding: 0; |
50 |
|
|
} |
51 |
|
|
li { |
52 |
|
|
display: block; |
53 |
|
|
margin: 0; |
54 |
|
|
padding: 0; |
55 |
|
|
clear: left; |
56 |
|
|
} |
57 |
|
|
li + li { |
58 |
|
|
margin-top: 0.3em; |
59 |
|
|
border-top: gray thin solid; |
60 |
|
|
padding-top: 0.2em; |
61 |
|
|
} |
62 |
|
|
</style>]; |
63 |
wakaba |
1.1 |
print q[<ul>]; |
64 |
|
|
print q[<li><a href="client.html?mode=editor">new</a>]; |
65 |
|
|
|
66 |
|
|
for my $date (reverse get_dates) { |
67 |
wakaba |
1.2 |
printf q[<li><img src="canvas-data/data-%s.txt.png"> %s <a href="client.html?mode=viewer;date=%s">view</a> <a href="client.html?mode=editor;date=%s">edit</a> <a href="client.html?mode=editor;import-date=%s">clone</a> <a href="canvas-data/data-%s.txt">data</a>], |
68 |
|
|
$date, |
69 |
wakaba |
1.1 |
(scalar localtime ($date / 1000)), |
70 |
|
|
$date, |
71 |
wakaba |
1.2 |
$date, |
72 |
|
|
$date, |
73 |
wakaba |
1.1 |
$date; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
print q[</ul>]; |
77 |
|
|
} elsif ($param->{mode} eq 'next') { |
78 |
|
|
for (get_dates) { |
79 |
|
|
next if $_ <= $param->{date}; |
80 |
|
|
my $mode = $ENV{HTTP_REFERER} =~ /viewer/ ? 'viewer' : 'editor'; |
81 |
|
|
my $url = qq<http://$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}/../client.html?mode=$mode;date=$_>; |
82 |
|
|
$url =~ s/[^\x21-\x7E]/_/g; |
83 |
|
|
print "Status: 302 Found\nLocation: $url\n\n"; |
84 |
|
|
} |
85 |
|
|
print "Status: 302 Found\nLocation: http://$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}/../client.html?mode=editor\n\n"; |
86 |
|
|
} elsif ($param->{mode} eq 'prev') { |
87 |
|
|
my $date; |
88 |
|
|
for (get_dates) { |
89 |
|
|
last if $_ >= $param->{date}; |
90 |
|
|
$date = $_; |
91 |
|
|
} |
92 |
|
|
if (defined $date) { |
93 |
|
|
my $mode = $ENV{HTTP_REFERER} =~ /viewer/ ? 'viewer' : 'editor'; |
94 |
|
|
my $url = qq<http://$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}/../client.html?mode=$mode;date=$date>; |
95 |
|
|
$url =~ s/[^\x21-\x7E]/_/g; |
96 |
|
|
print "Status: 302 Found\nLocation: $url\n\n"; |
97 |
|
|
} else { |
98 |
|
|
print "Status: 204 Not Found\n\n"; |
99 |
|
|
} |
100 |
wakaba |
1.2 |
} elsif ($param->{mode} eq 'png' and $ENV{REQUEST_METHOD} eq 'POST') { |
101 |
|
|
die if $ENV{CONTENT_LENGTH} > 100_000; |
102 |
|
|
|
103 |
|
|
read STDIN, my $url, $ENV{CONTENT_LENGTH}; |
104 |
|
|
die unless $url =~ s[^data:image/png;base64,][]; |
105 |
|
|
|
106 |
|
|
require MIME::Base64; |
107 |
|
|
my $png = MIME::Base64::decode_base64 ($url); |
108 |
|
|
|
109 |
|
|
open my $data_file, '>', $data_file_name . '.png' or die "$0: $data_file_name.png: $!"; |
110 |
|
|
print $data_file $png; |
111 |
|
|
|
112 |
|
|
my $l_url = qq<http://$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}/../$data_file_name.png>; |
113 |
|
|
print "Status: 201 Created\nLocation: $l_url\nContent-Type: text/plain\n\n"; |
114 |
wakaba |
1.1 |
} else { |
115 |
|
|
print "Content-Type: text/plain\n\n"; |
116 |
|
|
open my $data_file, '<', $data_file_name or die "$0: $data_file_name: $!"; |
117 |
|
|
while (<$data_file>) { |
118 |
|
|
print $_; |
119 |
|
|
} |
120 |
|
|
} |