Add blog media tab.

master
Blog Manager Robot 3 years ago
parent 46f28bacce
commit a6697bfc7b
  1. 27
      Web/lib/MJB/Web.pm
  2. 76
      Web/lib/MJB/Web/Controller/Dashboard.pm
  3. 2
      Web/templates/dashboard/_blog_nav.html.ep
  4. 63
      Web/templates/dashboard/blog_media.html.ep
  5. 76
      libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.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' );

@ -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;

@ -44,6 +44,6 @@
<a class="nav-link <%= $page eq 'builds' ? 'active' : '' %>" href="<%= $c->url_for( 'show_dashboard_blog_builds' ) %>">Builds</a>
</li>
<li class="nav-item">
<a class="nav-link <%= $page eq 'hooks' ? 'active' : '' %>" href="<%= $c->url_for( 'show_dashboard_blog_hooks' ) %>">Hooks</a>
<a class="nav-link <%= $page eq 'media' ? 'active' : '' %>" href="<%= $c->url_for( 'show_dashboard_blog_media' ) %>">Media</a>
</li>
</ul>

@ -0,0 +1,63 @@
% layout 'standard', title => 'Dashboard', sb_active => 'dashboard';
%= include 'dashboard/_blog_nav', page => 'media'
% if ( my $confirmation = flash 'confirmation' ) {
<div style="margin-top: 2em" class="alert alert-success" role="alert">
<%== $confirmation %>
</div>
% }
% if ( $c->stash->{success} ) {
<div style="margin-top: 2em" class="alert alert-success" role="alert">
<%= $c->stash->{success_message} %>
</div>
% }
% if ( $c->stash->{errors} ) {
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
There were errors with your request that could not be resolved:
<ul>
% for my $error ( @{$c->stash->{errors}} ) {
<li><%= $error %></li>
% }
</ul>
</div>
% }
<form method="POST" enctype="multipart/form-data" action="<%= $c->url_for( 'do_dashboard_blog_media' ) %>">
<div class="mt-3 mb-3">
<label for="upload" class="col-sm-2 col-form-label">File</label>
<input type="file" class="form-control" id="upload" name="upload" >
</div>
<div class="mb-3">
<input type="submit" class="btn btn-primary btn-sm float-end" width="100%" value="Upload File">
</div>
</form>
% if ( $media_files ) {
<table style="border: 1px solid #ccc" class="table mt-5">
<tbody>
<tr>
<th class="text-nowrap">Image</th>
<th class="text-nowrap">Embedd Link</th>
<th class="text-nowrap">Delete</th>
</tr>
</thead>
<tbody>
% for my $media_file ( @{$media_files} ) {
<tr>
<td><img width="250px" src="<%= $media_file->{url} %>"></img></td>
<td><%= $media_file->{markdown} %></td>
<td>
<form style="margin-top: 1.5em" method="POST" action="<%= $c->url_for( 'do_dashboard_blog_media_remove', { file => $media_file->{filename} } ) %>">
<input type="hidden" name="setting" value="webroot">
<button type="submit" class="btn btn-sm btn-primary float-end">Remove Media File</button>
</form>
</td>
</tr>
% }
</tbody>
</table>
% }

@ -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 ) = @_;

Loading…
Cancel
Save