mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-23 01:09:26 +02:00
Add:Item metadata utils config page for managing tags #1163
This commit is contained in:
parent
54b41b15c2
commit
4d93e39fa9
8 changed files with 287 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
|||
const Path = require('path')
|
||||
const fs = require('../libs/fsExtra')
|
||||
const Logger = require('../Logger')
|
||||
const SocketAuthority = require('../SocketAuthority')
|
||||
|
||||
const filePerms = require('../utils/filePerms')
|
||||
const patternValidation = require('../libs/nodeCron/pattern-validation')
|
||||
const { isObject } = require('../utils/index')
|
||||
|
@ -124,12 +126,13 @@ class MiscController {
|
|||
res.json(userResponse)
|
||||
}
|
||||
|
||||
// GET: api/tags
|
||||
getAllTags(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user attempted to getAllTags`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
var tags = []
|
||||
const tags = []
|
||||
this.db.libraryItems.forEach((li) => {
|
||||
if (li.media.tags && li.media.tags.length) {
|
||||
li.media.tags.forEach((tag) => {
|
||||
|
@ -142,6 +145,73 @@ class MiscController {
|
|||
})
|
||||
}
|
||||
|
||||
// POST: api/tags/rename
|
||||
async renameTag(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user attempted to renameTag`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
const tag = req.body.tag
|
||||
const newTag = req.body.newTag
|
||||
if (!tag || !newTag) {
|
||||
Logger.error(`[MiscController] Invalid request body for renameTag`)
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
let tagMerged = false
|
||||
let numItemsUpdated = 0
|
||||
|
||||
for (const li of this.db.libraryItems) {
|
||||
if (!li.media.tags || !li.media.tags.length) continue
|
||||
|
||||
if (li.media.tags.includes(newTag)) tagMerged = true // new tag is an existing tag so this is a merge
|
||||
|
||||
if (li.media.tags.includes(tag)) {
|
||||
li.media.tags = li.media.tags.filter(t => t !== tag) // Remove old tag
|
||||
if (!li.media.tags.includes(newTag)) {
|
||||
li.media.tags.push(newTag) // Add new tag
|
||||
}
|
||||
Logger.debug(`[MiscController] Rename tag "${tag}" to "${newTag}" for item "${li.media.metadata.title}"`)
|
||||
await this.db.updateLibraryItem(li)
|
||||
SocketAuthority.emitter('item_updated', li.toJSONExpanded())
|
||||
numItemsUpdated++
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
tagMerged,
|
||||
numItemsUpdated
|
||||
})
|
||||
}
|
||||
|
||||
// DELETE: api/tags/:tag
|
||||
async deleteTag(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user attempted to deleteTag`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
const tag = Buffer.from(decodeURIComponent(req.params.tag), 'base64').toString()
|
||||
|
||||
let numItemsUpdated = 0
|
||||
for (const li of this.db.libraryItems) {
|
||||
if (!li.media.tags || !li.media.tags.length) continue
|
||||
|
||||
if (li.media.tags.includes(tag)) {
|
||||
li.media.tags = li.media.tags.filter(t => t !== tag)
|
||||
Logger.debug(`[MiscController] Remove tag "${tag}" from item "${li.media.metadata.title}"`)
|
||||
await this.db.updateLibraryItem(li)
|
||||
SocketAuthority.emitter('item_updated', li.toJSONExpanded())
|
||||
numItemsUpdated++
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
numItemsUpdated
|
||||
})
|
||||
}
|
||||
|
||||
validateCronExpression(req, res) {
|
||||
const expression = req.body.expression
|
||||
if (!expression) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue