3 use App::IdiotBox::Common qw(@SupportedFormats);
4 use Web::Simple __PACKAGE__;
7 use HTML::Zoom::FilterBuilder::Template;
8 use List::Util qw(first);
10 use App::IdiotBox::Announcement;
11 use App::IdiotBox::Bucket;
12 use App::IdiotBox::Video;
14 has $_ => (is => 'ro') for qw(recent_announcements buckets);
17 template_dir => 'share/html',
19 db_file => 'var/lib/idiotbox.db',
20 base_url => 'http://localhost:3000/',
21 base_dir => do { use FindBin; $FindBin::Bin },
27 ($store = $self->config->{store}) =~ /^(\w+)$/
28 or die "Store config should be just a name, got ${store} instead";
29 my $store_class = "App::IdiotBox::Store::${store}";
30 eval "require ${store_class}; 1"
31 or die "Couldn't load ${store} store: $@";
32 $store_class->bind($self);
35 sub dispatch_request {
37 sub (/) { $self->show_front_page },
41 sub (%new_name=&new_slug=) {
42 my ($self, $name, $slug) = @_;
44 my ($nb, $err) = App::IdiotBox::Bucket->create(
49 return $self->show_admin_page(message => $err) if $err;
51 my $nb = $self->buckets->add($nb);
53 $self->show_admin_page(message => "New bucket created");
56 sub (/admin/) { $self->show_admin_page },
58 sub (/admin/bucket/*/...) {
59 my $bucket = $self->buckets->get({ slug => $_[1] });
63 my $video = $bucket->videos->get({ slug => $_[1] });
65 sub (POST + %edit_video=&*) {
66 my ($self, undef, $args) = @_;
67 delete $args->{edit_video};
69 my ($uv, $err) = $video->update(%$args);
71 return $self->show_edit_video_page($video, message => $err) if $err;
73 $bucket->videos->replace($video, $uv);
75 $self->show_edit_video_page($video, message => "Video updated");
78 sub (/) { $self->show_edit_video_page($video); }
81 sub (POST + %edit_bucket=&*) {
82 my ($self, undef, $args) = @_;
83 delete $args->{edit_bucket};
85 my ($ub, $err) = $bucket->update(%$args);
87 return $self->show_admin_page(message => $err) if $err;
89 $self->buckets->replace($bucket, $ub);
91 $self->show_bucket_edited_page($bucket);
94 $self->show_edit_bucket_page($bucket);
97 $self->show_confirm_delete_bucket_page($bucket)
100 $self->buckets->remove({ slug => $bucket->slug });
101 $self->show_bucket_deleted_page($bucket->slug);
107 my $bucket = $self->buckets->get({ slug => $_[1] });
109 $self->show_bucket($bucket)
112 $self->show_video($bucket->videos->get({ slug => $_[1] }));
117 sub show_front_page {
119 my $ann = $self->recent_announcements;
120 $self->html_response(
122 $_->select('#announcement-list')
123 ->repeat_content($ann->map(sub {
126 $_->select('.bucket-name')->replace_content($obj->bucket->name)
127 ->select('.made-at')->replace_content($obj->made_at)
128 ->select('.bucket-link')->set_attribute(
129 'href' => $obj->bucket->slug.'/'
131 ->select('.new-videos')->replace_content($obj->video_count)
132 ->select('.total-videos')->replace_content(
133 $obj->bucket->video_count
141 sub show_admin_page {
144 my $message = $opts{message} || '';
146 my $bucket = $self->buckets;
147 $self->html_response(
149 $_->select('#bucket-list')
150 ->repeat_content($bucket->map(sub {
153 $_->select('.bucket-slug')->replace_content($obj->slug)
154 ->select('.bucket-name')->replace_content($obj->name)
155 ->select('.edit-link')->set_attribute(
156 'href' => 'bucket/'.$obj->slug.'/'
158 ->select('.delete-link')->set_attribute(
159 'href' => 'bucket/'.$obj->slug.'/delete/'
163 ->select('.message-text')->replace_content($message)
170 sub show_confirm_delete_bucket_page {
171 my ($self, $bucket) = @_;
172 $self->html_response('delete_bucket' => sub {
173 $_->select('.bucket-name')->replace_content($bucket->name)
174 ->select('.confirm-yes')->set_attribute(
180 sub show_edit_bucket_page {
181 my ($self, $bucket, %opt) = @_;
182 my $message = $opt{message} || '';
183 $self->html_response('edit_bucket' => sub {
184 $_->select('.bucket-name')->replace_content($bucket->name)
185 ->select('.bucket-slug')->replace_content($bucket->slug)
186 ->select('.message-text')->replace_content($message)
187 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
190 $_->select('.video-name')->replace_content($video->name)
191 ->select('.video-author')->replace_content($video->author)
192 ->select('.video-link')->set_attribute(
193 href => 'video/'.$video->slug.'/'
201 sub show_edit_video_page {
202 my ($self, $video, %opt) = @_;
203 my $message = $opt{message} || '';
204 $self->html_response('edit_video' => sub {
205 $_->select('.video-name')->set_attribute(value => $video->name)
206 ->select('.author-name')->set_attribute(value => $video->author)
207 ->select('.bucket-slug')->replace_content($video->bucket->slug)
208 ->select('.slug-name')->replace_content($video->slug)
209 ->select('.message-text')->replace_content($message)
213 sub show_bucket_deleted_page {
214 my ($self, $name) = @_;
215 $self->html_response('deleted_bucket' => sub {
216 $_->select('.bucket-name')->replace_content($name)
220 sub show_bucket_edited_page {
221 my ($self, $name) = @_;
222 $self->html_response('edited_bucket' => sub {
223 $_->select('.bucket-name')->replace_content($name)
228 my ($self, $bucket) = @_;
229 $self->html_response(bucket => sub {
230 $_->select('.bucket-name')->replace_content($bucket->name)
231 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
234 $_->select('.video-name')->replace_content($video->name)
235 ->select('.video-author')->replace_content($video->author)
236 ->select('.video-link')->set_attribute(
237 href => $video->slug.'/'
245 my ($self, $video) = @_;
246 my $video_file = first {
247 -e join('/', $self->config->{base_dir}, $_)
249 join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
251 $self->html_response(video => sub {
254 .($video_file||'NO FILE FOUND SORRY');
256 $_->select('.video-name')->replace_content($video->name)
257 ->select('.author-name')->replace_content($video->author)
258 ->select('.bucket-link')->set_attribute(
261 ->select('.bucket-name')->replace_content($video->bucket->name)
262 ->select('.video-details')->replace_content($video->details)
263 ->select('script')->template_text_raw({ video_url => $video_url });
268 my ($self, $template_name, $selectors) = @_;
269 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
270 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
273 sub _template_filename_for {
274 my ($self, $name) = @_;
275 $self->{config}{template_dir}.'/'.$name.'.html';
280 $self->{layout_zoom} ||= HTML::Zoom->from_file(
281 $self->_template_filename_for('layout')
286 my ($self, $template_name, $selectors) = @_;
287 ($self->{zoom_for_template}{$template_name} ||= do {
289 HTML::Zoom->from_file(
290 $self->_template_filename_for($template_name)
292 ->select('#main-content')->collect_content({ into => \@body })
295 ->select('#main-content')->replace_content(\@body)
297 })->apply($selectors);
302 $self->{base_url} ||= do {
303 (my $u = $self->config->{base_url}) =~ s/\/$//;
310 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
311 return $self->SUPER::_run_cli(@_);
318 "To import data into your idiotbox install, chdir into a directory\n".
319 "containing video files and run:\n".
323 $self->SUPER::_cli_usage(@_);
328 require App::IdiotBox::Importer;
329 App::IdiotBox::Importer->run($self);