diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index fb6b803..831ab44 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -114,18 +114,21 @@ sub startup ($self) { $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); # Dashboard - $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); - $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); - $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); - $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); - $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); - $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); - $auth->post( '/dashboard/blog/:id/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' ); - $auth->get ( '/dashboard/blog/:id/settings' )->to('Dashboard#blog_settings' )->name('show_dashboard_blog_settings' ); - $auth->post( '/dashboard/blog/:id/settings' )->to('Dashboard#do_blog_settings' )->name('do_dashboard_blog_settings' ); - $auth->get ( '/dashboard/blog/:id/config' )->to('Dashboard#blog_config' )->name('show_dashboard_blog_config' ); - $auth->post( '/dashboard/blog/:id/config' )->to('Dashboard#do_blog_config' )->name('do_dashboard_blog_config' ); - $auth->get ( '/dashboard/blog/:id/builds' )->to('Dashboard#blog_builds' )->name('show_dashboard_blog_builds' ); + $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); + $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); + $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); + $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); + $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); + $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); + $auth->post( '/dashboard/blog/:id/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' ); + $auth->get ( '/dashboard/blog/:id/settings' )->to('Dashboard#blog_settings' )->name('show_dashboard_blog_settings' ); + $auth->post( '/dashboard/blog/:id/settings' )->to('Dashboard#do_blog_settings' )->name('do_dashboard_blog_settings' ); + $auth->get ( '/dashboard/blog/:id/config' )->to('Dashboard#blog_config' )->name('show_dashboard_blog_config' ); + $auth->post( '/dashboard/blog/:id/config' )->to('Dashboard#do_blog_config' )->name('do_dashboard_blog_config' ); + $auth->get ( '/dashboard/blog/:id/builds' )->to('Dashboard#blog_builds' )->name('show_dashboard_blog_builds' ); + $auth->get ( '/dashboard/blog/:id/media' )->to('Dashboard#blog_media' )->name('show_dashboard_blog_media' ); + $auth->post( '/dashboard/blog/:id/media' )->to('Dashboard#do_blog_media' )->name('do_dashboard_blog_media' ); + $auth->post( '/dashboard/blog/:id/media/*file' )->to('Dashboard#do_blog_media_remove' )->name('do_dashboard_blog_media_remove' ); # Blog Management $auth->get ( '/blog' )->to('Blog#create' )->name('show_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index b4e7337..6b7a463 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -261,6 +261,82 @@ sub blog_builds ( $c ) { } +sub blog_media ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $media_files = $c->stash->{media_files} = $c->jekyll($blog->domain->name)->list_media; +} + +sub do_blog_media_remove( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $jekyll = $c->jekyll($blog->domain->name); + my $media_file = Mojo::File->new( $jekyll->repo_path . "/assets/media/" . $c->param('file') ); + + if ( $media_file->stat ) { + $jekyll->remove_file( $media_file->to_string, "Removed media file" . $media_file->basename ); + } + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ] ); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "Removed " . $media_file->basename ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_media', { id => $blog->id } ) ); + +} + +sub do_blog_media ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $jekyll = $c->jekyll($blog->domain->name); + + my $upload = $c->req->upload( 'upload' ); + + # Ensure the upload directory exists. + Mojo::File->new( $jekyll->repo_path . "/assets/media/" )->make_path; + + # Move the file there + $upload->move_to( $jekyll->repo_path . "/assets/media/" . $upload->filename ); + + $jekyll->commit_file( + $jekyll->repo_path . "/assets/media/" . $upload->filename, + "Add media " . $upload->filename + ); + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ] ); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "Uploaded file!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_media', { id => $blog->id } ) ); +} + 1; diff --git a/Web/templates/dashboard/_blog_nav.html.ep b/Web/templates/dashboard/_blog_nav.html.ep index 6e986c9..3c80a09 100644 --- a/Web/templates/dashboard/_blog_nav.html.ep +++ b/Web/templates/dashboard/_blog_nav.html.ep @@ -44,6 +44,6 @@ Builds diff --git a/Web/templates/dashboard/blog_media.html.ep b/Web/templates/dashboard/blog_media.html.ep new file mode 100644 index 0000000..b6b4093 --- /dev/null +++ b/Web/templates/dashboard/blog_media.html.ep @@ -0,0 +1,63 @@ +% layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; + +%= include 'dashboard/_blog_nav', page => 'media' + +% if ( my $confirmation = flash 'confirmation' ) { + +% } + +% if ( $c->stash->{success} ) { + +% } + +% if ( $c->stash->{errors} ) { + +% } + +
+
+ + +
+ +
+ +
+
+ +% if ( $media_files ) { + + + + + + + + + + % for my $media_file ( @{$media_files} ) { + + + + + + % } + +
ImageEmbedd LinkDelete
<%= $media_file->{markdown} %> +
+ + +
+
+% } diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index bf28d1c..bbe85c4 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -91,6 +91,28 @@ sub init { return $self; } +sub list_media { + my ( $self ) = @_; + + $self->_ensure_repository_is_latest; + + my $media = Mojo::File->new( $self->repo_path . "/assets/media" ); + + my @return; + + # TODO: Sort by date for the listing on the front end. + foreach my $file ( $media->list->each ) { + push @return, { + path => $file->to_string, + filename => $file->basename, + url => 'https://' . $self->domain . '/assets/media/' . $file->basename, + markdown => '![Title for image](https://' . $self->domain . '/assets/media/' . $file->basename . ')', + }; + } + + return [ @return ]; +} + sub list_posts { my ( $self ) = @_; @@ -185,7 +207,11 @@ sub write_config { sub write_post { my ( $self, $md_file ) = @_; + + # Check that the repo exists and is latest. + $self->_ensure_repository_is_latest; + # Write the file $md_file->write; # Add the file to git @@ -206,6 +232,56 @@ sub write_post { return 1; } +sub commit_file { + my ( $self, $file, $comment ) = @_; + + # Check that the repo exists and is latest. + $self->_ensure_repository_is_latest; + + # Add the file to git + $self->system_command( [ qw( git add ), $file ], { + chdir => $self->repo_path, + }); + + # Commit the file + $self->system_command( [ qw( git commit -m ), $comment ], { + chdir => $self->repo_path, + }); + + # Push the repo to the store server + $self->system_command( [ qw( git push origin master ) ], { + chdir => $self->repo_path, + }); + + return 1; + +} + +sub remove_file { + my ( $self, $file, $comment ) = @_; + + # Check that the repo exists and is latest. + $self->_ensure_repository_is_latest; + + # Add the file to git + $self->system_command( [ qw( git rm ), $file ], { + chdir => $self->repo_path, + }); + + # Commit the file + $self->system_command( [ qw( git commit -m ), $comment ], { + chdir => $self->repo_path, + }); + + # Push the repo to the store server + $self->system_command( [ qw( git push origin master ) ], { + chdir => $self->repo_path, + }); + + return 1; + +} + sub delete_post { my ( $self, $title, $file ) = @_;