feat: list albums' total size and zip size

This commit is contained in:
Bobby Wibowo 2021-02-12 15:48:40 +07:00
parent 5d9d499642
commit cf4a1af209
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
4 changed files with 55 additions and 9 deletions

View File

@ -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)
}
}

View File

@ -16,6 +16,7 @@ const fsFuncs = [
'readFile',
'rename',
'rmdir',
'stat',
'symlink',
'unlink',
'writeFile'

View File

@ -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 **/

View File

@ -1757,7 +1757,10 @@ page.getAlbums = (params = {}) => {
<th>Name</th>
${params.all ? '<th>User</th>' : ''}
<th>Uploads</th>
<th>Size</th>
<th>Created at</th>
<th>ZIP size</th>
<th>ZIP generated at</th>
<th>Public link</th>
<th class="has-text-right">(${response.data.count} total)</th>
</tr>
@ -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 = {}) => {
<th${enabled ? '' : ' class="has-text-grey"'}>${album.name}</td>
${params.all ? `<th>${album.userid ? (users[album.userid] || '') : ''}</th>` : ''}
<th>${album.uploads}</th>
<td>${page.getPrettyBytes(album.size)}</td>
<td>${album.prettyDate}</td>
<td><a ${enabled && album.public ? '' : 'class="is-linethrough" '}href="${albumUrl}" target="_blank">${albumUrl}</a></td>
<td>${album.hasZip ? page.getPrettyBytes(album.zipSize) : '-'}</td>
<td${album.isZipExpired ? ' class="has-text-warning" title="This album has been modified since the last time its ZIP was generated."' : ''}>${album.hasZip ? album.prettyZipDate : '-'}</td$>
<td><a ${enabled && album.public ? '' : 'class="is-linethrough" '}href="${albumUrl}" target="_blank">${albumUrlText}</a></td>
<td class="has-text-right" data-id="${album.id}">
<a class="button is-small is-primary is-outlined" title="Edit album" data-action="edit-album">
<span class="icon is-small">
@ -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 = `
<div class="field">
@ -1915,6 +1932,9 @@ page.editAlbum = id => {
</label>
</div>
</div>
<div class="field">
<p>Current public link: <a href="${albumUrl}" target="_blank" class="is-underline">${albumUrlText}</a></p>
</div>
`
swal({