diff --git a/public/js/admin.js b/public/js/admin.js index 5c9051d..283d728 100644 --- a/public/js/admin.js +++ b/public/js/admin.js @@ -1,40 +1,88 @@ -let scanning = false; - -const scan = () => { - scanning = true; - $('#scan-status > div').removeAttr('hidden'); - $('#scan-status > span').attr('hidden', ''); - const color = $('#scan').css('color'); - $('#scan').css('color', 'gray'); - $.post(base_url + 'api/admin/scan', (data) => { - const ms = data.milliseconds; - const titles = data.titles; - $('#scan-status > span').text('Scanned ' + titles + ' titles in ' + ms + 'ms'); - $('#scan-status > span').removeAttr('hidden'); - $('#scan').css('color', color); - $('#scan-status > div').attr('hidden', ''); - scanning = false; - }); -} - -String.prototype.capitalize = function() { - return this.charAt(0).toUpperCase() + this.slice(1); -} - $(() => { - $('li').click((e) => { - const url = $(e.currentTarget).attr('data-url'); - if (url) { - $(location).attr('href', url); - } - }); - const setting = loadThemeSetting(); - $('#theme-select').val(setting.capitalize()); - + $('#theme-select').val(capitalize(setting)); $('#theme-select').change((e) => { const newSetting = $(e.currentTarget).val().toLowerCase(); saveThemeSetting(newSetting); setTheme(); }); + + setInterval(getProgress, 5000); }); + +/** + * Capitalize String + * + * @function capitalize + * @param {string} str - The string to be capitalized + * @return {string} The capitalized string + */ +const capitalize = (str) => { + return str.charAt(0).toUpperCase() + str.slice(1); +}; + +/** + * Set an alpine.js property + * + * @function setProp + * @param {string} key - Key of the data property + * @param {*} prop - The data property + */ +const setProp = (key, prop) => { + $('#root').get(0).__x.$data[key] = prop; +}; + +/** + * Get an alpine.js property + * + * @function getProp + * @param {string} key - Key of the data property + * @return {*} The data property + */ +const getProp = (key) => { + return $('#root').get(0).__x.$data[key]; +}; + +/** + * Get the thumbnail generation progress from the API + * + * @function getProgress + */ +const getProgress = () => { + $.get(`${base_url}api/admin/thumbnail_progress`) + .then(data => { + setProp('progress', data.progress); + const generating = data.progress > 0 + setProp('generating', generating); + }); +}; + +/** + * Trigger the thumbnail generation + * + * @function generateThumbnails + */ +const generateThumbnails = () => { + setProp('generating', true); + setProp('progress', 0.0); + $.post(`${base_url}api/admin/generate_thumbnails`); +}; + +/** + * Trigger the scan + * + * @function scan + */ +const scan = () => { + setProp('scanning', true); + setProp('scanMs', -1); + setProp('scanTitles', 0); + $.post(`${base_url}api/admin/scan`) + .then(data => { + setProp('scanMs', data.milliseconds); + setProp('scanTitles', data.titles); + }) + .always(() => { + setProp('scanning', false); + }); +} diff --git a/src/library/library.cr b/src/library/library.cr index 8d9218e..ce2b1be 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -221,10 +221,16 @@ class Library end def thumbnail_generation_progress + return 0 if @entries_count == 0 @thumbnails_count / @entries_count end def generate_thumbnails + if @thumbnails_count > 0 + Logger.debug "Thumbnail generation in progress" + return + end + Logger.info "Starting thumbnail generation" entries = deep_titles.map(&.deep_entries).flatten.reject &.err_msg @entries_count = entries.size @@ -233,10 +239,18 @@ class Library # Report generation progress regularly spawn do loop do - break if thumbnail_generation_progress.to_i == 1 - Logger.debug "Thumbnail generation progress: " \ - "#{(thumbnail_generation_progress * 100).round 1}%" - sleep 30.seconds + unless @thumbnails_count == 0 + Logger.debug "Thumbnail generation progress: " \ + "#{(thumbnail_generation_progress * 100).round 1}%" + end + # Generation is completed. We reset the count to 0 to allow subsequent + # calls to the function, and break from the loop to stop the progress + # report fiber + if thumbnail_generation_progress.to_i == 1 + @thumbnails_count = 0 + break + end + sleep 10.seconds end end @@ -249,8 +263,6 @@ class Library end @thumbnails_count += 1 end - Logger.info "Thumbnail generation finished. " \ - "#{@thumbnails_count}/#{@entries_count} " \ - "thumbnails generated" + Logger.info "Thumbnail generation finished" end end diff --git a/src/routes/api.cr b/src/routes/api.cr index 5a5a506..ce2caf5 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -76,6 +76,18 @@ class APIRouter < Router }.to_json end + get "/api/admin/thumbnail_progress" do |env| + send_json env, { + "progress" => Library.default.thumbnail_generation_progress, + }.to_json + end + + post "/api/admin/generate_thumbnails" do |env| + spawn do + Library.default.generate_thumbnails + end + end + post "/api/admin/user/delete/:username" do |env| begin username = env.params.url["username"] diff --git a/src/views/admin.html.ecr b/src/views/admin.html.ecr index 456dff4..a0959bf 100644 --- a/src/views/admin.html.ecr +++ b/src/views/admin.html.ecr @@ -1,11 +1,17 @@ -