From cf4a1af209b2bd70719c801b932e204d7a1d41ae Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 12 Feb 2021 15:48:40 +0700 Subject: [PATCH] feat: list albums' total size and zip size --- controllers/albumsController.js | 20 ++++++++++++++++++-- controllers/pathsController.js | 1 + src/css/dashboard.scss | 17 +++++++++++++---- src/js/dashboard.js | 26 +++++++++++++++++++++++--- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index af4ed5b..07d4743 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -111,7 +111,7 @@ self.list = async (req, res, next) => { if (isNaN(offset)) offset = 0 else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) - fields.push('identifier', 'enabled', 'timestamp', 'editedAt', 'download', 'public', 'description') + fields.push('identifier', 'enabled', 'timestamp', 'editedAt', 'zipGeneratedAt', 'download', 'public', 'description') if (all) fields.push('userid') albums = await db.table('albums') @@ -126,18 +126,34 @@ self.list = async (req, res, next) => { album.download = album.download !== 0 album.public = album.public !== 0 album.uploads = 0 + album.size = 0 + album.zipSize = null // Map by IDs albumids[album.id] = album } + const getAlbumZipSize = async album => { + if (!album.zipGeneratedAt) return + try { + const filePath = path.join(paths.zips, `${album.identifier}.zip`) + const stats = await paths.stat(filePath) + albumids[album.id].zipSize = stats.size + } catch (error) { + if (error.code !== 'ENOENT') logger.error(error) + } + } + + await Promise.all(albums.map(album => getAlbumZipSize(album))) + const uploads = await db.table('files') .whereIn('albumid', Object.keys(albumids)) - .select('albumid') + .select('albumid', 'size') for (const upload of uploads) { if (albumids[upload.albumid]) { albumids[upload.albumid].uploads++ + albumids[upload.albumid].size += parseInt(upload.size) } } diff --git a/controllers/pathsController.js b/controllers/pathsController.js index f1bc29a..18d38ea 100644 --- a/controllers/pathsController.js +++ b/controllers/pathsController.js @@ -16,6 +16,7 @@ const fsFuncs = [ 'readFile', 'rename', 'rmdir', + 'stat', 'symlink', 'unlink', 'writeFile' diff --git a/src/css/dashboard.scss b/src/css/dashboard.scss index e2ab6fa..bc516e1 100644 --- a/src/css/dashboard.scss +++ b/src/css/dashboard.scss @@ -153,10 +153,19 @@ li[data-action="page-ellipsis"] { color: $grey-lightest } -.table .originalname { - max-width: 200px; - text-overflow: ellipsis; - overflow: hidden +.table { + .originalname { + max-width: 200px; + text-overflow: ellipsis; + overflow: hidden + } + + th, + td { + &[title] { + cursor: help + } + } } /** Thumbs extension **/ diff --git a/src/js/dashboard.js b/src/js/dashboard.js index a3276cd..eea13ef 100644 --- a/src/js/dashboard.js +++ b/src/js/dashboard.js @@ -1757,7 +1757,10 @@ page.getAlbums = (params = {}) => { Name ${params.all ? 'User' : ''} Uploads + Size Created at + ZIP size + ZIP generated at Public link (${response.data.count} total) @@ -1775,13 +1778,18 @@ page.getAlbums = (params = {}) => { for (let i = 0; i < albums.length; i++) { const album = albums[i] - const albumUrl = `${homeDomain}/a/${album.identifier}` + const urlPath = '/a/' + const albumUrlText = urlPath + album.identifier + const albumUrl = homeDomain + albumUrlText const selected = page.selected[page.currentView].includes(album.id) if (!selected) unselected = true // Prettify + album.hasZip = album.zipSize !== null album.prettyDate = page.getPrettyDate(new Date(album.timestamp * 1000)) + album.prettyZipDate = album.hasZip ? page.getPrettyDate(new Date(album.zipGeneratedAt * 1000)) : null + album.isZipExpired = album.hasZip && !(album.zipGeneratedAt > album.editedAt) // Server-side explicitly expect this value to consider an album as disabled const enabled = album.enabled !== 0 @@ -1790,7 +1798,10 @@ page.getAlbums = (params = {}) => { download: album.download, public: album.public, description: album.description, - enabled + enabled, + homeDomain, + urlPath, + identifier: album.identifier } const tr = document.createElement('tr') @@ -1801,8 +1812,11 @@ page.getAlbums = (params = {}) => { ${album.name} ${params.all ? `${album.userid ? (users[album.userid] || '') : ''}` : ''} ${album.uploads} + ${page.getPrettyBytes(album.size)} ${album.prettyDate} - ${albumUrl} + ${album.hasZip ? page.getPrettyBytes(album.zipSize) : '-'} + ${album.hasZip ? album.prettyZipDate : '-'} + ${albumUrlText} @@ -1867,6 +1881,9 @@ page.editAlbum = id => { const album = page.cache[id] if (!album) return + const albumUrlText = album.urlPath + album.identifier + const albumUrl = album.homeDomain + albumUrlText + const div = document.createElement('div') div.innerHTML = `
@@ -1915,6 +1932,9 @@ page.editAlbum = id => {
+
` swal({