Update uploader to support podcast folder structure

This commit is contained in:
advplyr 2022-04-14 18:24:24 -05:00
parent 5a26b01ffb
commit a62f7a4861
6 changed files with 186 additions and 138 deletions

View File

@ -1,7 +1,7 @@
<template>
<div class="relative w-full py-4 px-6 border border-white border-opacity-10 shadow-lg rounded-md my-6">
<div class="absolute -top-3 -left-3 w-8 h-8 bg-bg border border-white border-opacity-10 flex items-center justify-center rounded-full">
<p class="text-base text-white text-opacity-80 font-mono">#{{ book.index }}</p>
<p class="text-base text-white text-opacity-80 font-mono">#{{ item.index }}</p>
</div>
<div v-if="!processing && !uploadFailed && !uploadSuccess" class="absolute -top-3 -right-3 w-8 h-8 bg-bg border border-white border-opacity-10 flex items-center justify-center rounded-full hover:bg-error cursor-pointer" @click="$emit('remove')">
@ -15,15 +15,19 @@
<div class="flex my-2 -mx-2">
<div class="w-1/2 px-2">
<ui-text-input-with-label v-model="bookData.title" :disabled="processing" label="Title" @input="titleUpdated" />
<ui-text-input-with-label v-model="itemData.title" :disabled="processing" label="Title" @input="titleUpdated" />
</div>
<div class="w-1/2 px-2">
<ui-text-input-with-label v-model="bookData.author" :disabled="processing" label="Author" />
<ui-text-input-with-label v-if="!isPodcast" v-model="itemData.author" :disabled="processing" label="Author" />
<div v-else class="w-full">
<p class="px-1 text-sm font-semibold">Directory <em class="font-normal text-xs pl-2">(auto)</em></p>
<ui-text-input :value="directory" disabled class="w-full font-mono text-xs" style="height: 38px" />
</div>
</div>
</div>
<div class="flex my-2 -mx-2">
<div v-if="!isPodcast" class="flex my-2 -mx-2">
<div class="w-1/2 px-2">
<ui-text-input-with-label v-model="bookData.series" :disabled="processing" label="Series" note="(optional)" />
<ui-text-input-with-label v-model="itemData.series" :disabled="processing" label="Series" note="(optional)" />
</div>
<div class="w-1/2 px-2">
<div class="w-full">
@ -33,9 +37,9 @@
</div>
</div>
<tables-uploaded-files-table :files="book.bookFiles" title="Book Files" class="mt-8" />
<tables-uploaded-files-table v-if="book.otherFiles.length" title="Other Files" :files="book.otherFiles" />
<tables-uploaded-files-table v-if="book.ignoredFiles.length" title="Ignored Files" :files="book.ignoredFiles" />
<tables-uploaded-files-table :files="item.itemFiles" title="Item Files" class="mt-8" />
<tables-uploaded-files-table v-if="item.otherFiles.length" title="Other Files" :files="item.otherFiles" />
<tables-uploaded-files-table v-if="item.ignoredFiles.length" title="Ignored Files" :files="item.ignoredFiles" />
</template>
<widgets-alert v-if="uploadSuccess" type="success">
<p class="text-base">Successfully Uploaded!</p>
@ -55,15 +59,16 @@ import Path from 'path'
export default {
props: {
book: {
item: {
type: Object,
default: () => {}
},
mediaType: String,
processing: Boolean
},
data() {
return {
bookData: {
itemData: {
title: '',
author: '',
series: ''
@ -75,14 +80,19 @@ export default {
}
},
computed: {
isPodcast() {
return this.mediaType === 'podcast'
},
directory() {
if (!this.bookData.title) return ''
if (this.bookData.series && this.bookData.author) {
return Path.join(this.bookData.author, this.bookData.series, this.bookData.title)
} else if (this.bookData.author) {
return Path.join(this.bookData.author, this.bookData.title)
if (!this.itemData.title) return ''
if (this.isPodcast) return this.itemData.title
if (this.itemData.series && this.itemData.author) {
return Path.join(this.itemData.author, this.itemData.series, this.itemData.title)
} else if (this.itemData.author) {
return Path.join(this.itemData.author, this.itemData.title)
} else {
return this.bookData.title
return this.itemData.title
}
}
},
@ -96,24 +106,24 @@ export default {
this.error = ''
},
getData() {
if (!this.bookData.title) {
if (!this.itemData.title) {
this.error = 'Must have a title'
return null
}
this.error = ''
var files = this.book.bookFiles.concat(this.book.otherFiles)
var files = this.item.itemFiles.concat(this.item.otherFiles)
return {
index: this.book.index,
...this.bookData,
index: this.item.index,
...this.itemData,
files
}
}
},
mounted() {
if (this.book) {
this.bookData.title = this.book.title
this.bookData.author = this.book.author
this.bookData.series = this.book.series
if (this.item) {
this.itemData.title = this.item.title
this.itemData.author = this.item.author
this.itemData.series = this.item.series
}
}
}

View File

@ -4,8 +4,8 @@ export default {
data() {
return {
uploadHelpers: {
getBooksFromDrop: this.getBooksFromDataTransferItems,
getBooksFromPicker: this.getBooksFromFileList
getItemsFromDrop: this.getItemsFromDataTransferItems,
getItemsFromPicker: this.getItemsFromFilelist
}
}
},
@ -23,8 +23,8 @@ export default {
}
return false
},
filterAudiobookFiles(files) {
var validBookFiles = []
filterItemFiles(files, mediaType) {
var validItemFiles = []
var validOtherFiles = []
var ignoredFiles = []
files.forEach((file) => {
@ -32,60 +32,60 @@ export default {
if (!filetype) ignoredFiles.push(file)
else {
file.filetype = filetype
if (filetype === 'audio' || filetype === 'ebook') validBookFiles.push(file)
if (filetype === 'audio' || (filetype === 'ebook' && mediaType === 'book')) validItemFiles.push(file)
else validOtherFiles.push(file)
}
})
return {
bookFiles: validBookFiles,
itemFiles: validItemFiles,
otherFiles: validOtherFiles,
ignoredFiles
}
},
audiobookFromItems(items) {
var { bookFiles, otherFiles, ignoredFiles } = this.filterAudiobookFiles(items)
if (!bookFiles.length) {
itemFromTreeItems(items, mediaType) {
var { itemFiles, otherFiles, ignoredFiles } = this.filterItemFiles(items, mediaType)
if (!itemFiles.length) {
ignoredFiles = ignoredFiles.concat(otherFiles)
otherFiles = []
}
return [
{
bookFiles,
itemFiles,
otherFiles,
ignoredFiles
}
]
},
traverseForAudiobook(folder, depth = 1) {
traverseForItem(folder, mediaType, depth = 1) {
if (folder.items.some((f) => f.isDirectory)) {
var audiobooks = []
var items = []
folder.items.forEach((file) => {
if (file.isDirectory) {
var audiobookResults = this.traverseForAudiobook(file, ++depth)
audiobooks = audiobooks.concat(audiobookResults)
var itemResults = this.traverseForItem(file, mediaType, ++depth)
items = items.concat(itemResults)
}
})
return audiobooks
return items
} else {
return this.audiobookFromItems(folder.items)
return this.itemFromTreeItems(folder.items, mediaType)
}
},
fileTreeToAudiobooks(filetree) {
fileTreeToItems(filetree, mediaType) {
// Has directores - Is Multi Book Drop
if (filetree.some((f) => f.isDirectory)) {
var ignoredFilesInRoot = filetree.filter((f) => !f.isDirectory)
if (ignoredFilesInRoot.length) filetree = filetree.filter((f) => f.isDirectory)
var audiobookResults = this.traverseForAudiobook({ items: filetree })
var itemResults = this.traverseForItem({ items: filetree }, mediaType)
return {
audiobooks: audiobookResults,
items: itemResults,
ignoredFiles: ignoredFilesInRoot
}
} else {
// Single Book drop
return {
audiobooks: this.audiobookFromItems(filetree),
items: this.itemFromTreeItems(filetree, mediaType),
ignoredFiles: []
}
}
@ -140,7 +140,7 @@ export default {
series: '',
...book
}
var firstBookFile = book.bookFiles[0]
var firstBookFile = book.itemFiles[0]
if (!firstBookFile.filepath) return audiobook // No path
var firstBookPath = Path.dirname(firstBookFile.filepath)
@ -157,32 +157,49 @@ export default {
}
return audiobook
},
async getBooksFromDataTransferItems(items) {
cleanPodcast(item, index) {
var podcast = {
index,
title: '',
...item
}
var firstAudioFile = podcast.itemFiles[0]
if (!firstAudioFile.filepath) return podcast // No path
var firstPath = Path.dirname(firstAudioFile.filepath)
var dirs = firstPath.split('/').filter(d => !!d && d !== '.')
podcast.title = dirs.length > 1 ? dirs[1] : dirs[0]
return podcast
},
cleanItem(item, mediaType, index) {
if (mediaType === 'podcast') return this.cleanPodcast(item, index)
return this.cleanBook(item, index)
},
async getItemsFromDataTransferItems(items, mediaType) {
var files = await this.getFilesDropped(items)
if (!files || !files.length) return { error: 'No files found ' }
var audiobooksData = this.fileTreeToAudiobooks(files)
if (!audiobooksData.audiobooks.length && !audiobooksData.ignoredFiles.length) {
var itemData = this.fileTreeToItems(files, mediaType)
if (!itemData.items.length && !itemData.ignoredFiles.length) {
return { error: 'Invalid file drop' }
}
var ignoredFiles = audiobooksData.ignoredFiles
var ignoredFiles = itemData.ignoredFiles
var index = 1
var books = audiobooksData.audiobooks.filter((ab) => {
if (!ab.bookFiles.length) {
var items = itemData.items.filter((ab) => {
if (!ab.itemFiles.length) {
if (ab.otherFiles.length) ignoredFiles = ignoredFiles.concat(ab.otherFiles)
if (ab.ignoredFiles.length) ignoredFiles = ignoredFiles.concat(ab.ignoredFiles)
}
return ab.bookFiles.length
}).map(ab => this.cleanBook(ab, index++))
return ab.itemFiles.length
}).map(ab => this.cleanItem(ab, index++))
return {
books,
items,
ignoredFiles
}
},
getBooksFromFileList(filelist) {
getItemsFromFilelist(filelist, mediaType) {
var ignoredFiles = []
var otherFiles = []
var bookMap = {}
var itemMap = {}
filelist.forEach((file) => {
var filetype = this.checkFileType(file.name)
@ -191,17 +208,17 @@ export default {
file.filetype = filetype
if (file.webkitRelativePath) file.filepath = file.webkitRelativePath
if (filetype === 'audio' || filetype === 'ebook') {
if (filetype === 'audio' || (filetype === 'ebook' && mediaType === 'book')) {
var dir = file.filepath ? Path.dirname(file.filepath) : ''
if (!bookMap[dir]) {
bookMap[dir] = {
if (!itemMap[dir]) {
itemMap[dir] = {
path: dir,
ignoredFiles: [],
bookFiles: [],
itemFiles: [],
otherFiles: []
}
}
bookMap[dir].bookFiles.push(file)
itemMap[dir].itemFiles.push(file)
} else {
otherFiles.push(file)
}
@ -210,18 +227,18 @@ export default {
otherFiles.forEach((file) => {
var dir = Path.dirname(file.filepath)
var findBook = Object.values(bookMap).find(b => dir.startsWith(b.path))
if (findBook) {
bookMap[dir].otherFiles.push(file)
var findItem = Object.values(itemMap).find(b => dir.startsWith(b.path))
if (findItem) {
findItem.otherFiles.push(file)
} else {
ignoredFiles.push(file)
}
})
var index = 1
var books = Object.values(bookMap).map(ab => this.cleanBook(ab, index++))
var items = Object.values(itemMap).map(i => this.cleanItem(i, mediaType, index++))
return {
books,
items,
ignoredFiles: ignoredFiles
}
},

View File

@ -3,11 +3,14 @@
<div class="w-full max-w-6xl mx-auto">
<!-- Library & folder picker -->
<div class="flex my-6 -mx-2">
<div class="w-1/3 px-2">
<ui-dropdown v-model="selectedLibraryId" :items="libraryItems" label="Library" :disabled="processing" @input="libraryChanged" />
<div class="w-1/5 px-2">
<ui-dropdown v-model="selectedLibraryId" :items="libraryItems" label="Library" :disabled="!!items.length" @input="libraryChanged" />
</div>
<div class="w-2/3 px-2">
<ui-dropdown v-model="selectedFolderId" :items="folderItems" :disabled="!selectedLibraryId || processing" label="Folder" />
<div class="w-3/5 px-2">
<ui-dropdown v-model="selectedFolderId" :items="folderItems" :disabled="!selectedLibraryId || !!items.length" label="Folder" />
</div>
<div class="w-1/5 px-2">
<ui-text-input-with-label :value="selectedLibraryMediaType" readonly label="Media Type" />
</div>
</div>
@ -16,7 +19,7 @@
</widgets-alert>
<!-- Picker display -->
<div v-if="!books.length && !ignoredFiles.length" class="w-full mx-auto border border-white border-opacity-20 px-12 pt-12 pb-4 my-12 relative" :class="isDragging ? 'bg-primary bg-opacity-40' : 'border-dashed'">
<div v-if="!items.length && !ignoredFiles.length" class="w-full mx-auto border border-white border-opacity-20 px-12 pt-12 pb-4 my-12 relative" :class="isDragging ? 'bg-primary bg-opacity-40' : 'border-dashed'">
<p class="text-2xl text-center">{{ isDragging ? 'Drop files' : "Drag n' drop files or folders" }}</p>
<p class="text-center text-sm my-5">or</p>
<div class="w-full max-w-xl mx-auto">
@ -29,33 +32,33 @@
<p class="text-xs text-white text-opacity-50 font-mono"><strong>Supported File Types: </strong>{{ inputAccept.join(', ') }}</p>
</div>
</div>
<!-- Book list header -->
<!-- Item list header -->
<div v-else class="w-full flex items-center pb-4 border-b border-white border-opacity-10">
<p class="text-lg">{{ books.length }} book{{ books.length === 1 ? '' : 's' }}</p>
<p class="text-lg">{{ items.length }} item{{ items.length === 1 ? '' : 's' }}</p>
<p v-if="ignoredFiles.length" class="text-lg">&nbsp;|&nbsp;{{ ignoredFiles.length }} file{{ ignoredFiles.length === 1 ? '' : 's' }} ignored</p>
<div class="flex-grow" />
<ui-btn :disabled="processing" small @click="reset">Reset</ui-btn>
</div>
<!-- Alerts -->
<widgets-alert v-if="!books.length && !uploadReady" type="error" class="my-4">
<p class="text-lg">No books found</p>
<widgets-alert v-if="!items.length && !uploadReady" type="error" class="my-4">
<p class="text-lg">No items found</p>
</widgets-alert>
<widgets-alert v-if="ignoredFiles.length" type="warning" class="my-4">
<div class="w-full pr-12">
<p class="text-base mb-1">Unsupported files are ignored. When choosing or dropping a folder, other files that are not in a book folder are ignored.</p>
<p class="text-base mb-1">Unsupported files are ignored. When choosing or dropping a folder, other files that are not in an item folder are ignored.</p>
<tables-uploaded-files-table :files="ignoredFiles" title="Ignored Files" class="text-white" />
<p class="text-xs text-white text-opacity-50 font-mono pt-1"><strong>Supported File Types: </strong>{{ inputAccept.join(', ') }}</p>
</div>
</widgets-alert>
<!-- Book Upload cards -->
<template v-for="(book, index) in books">
<cards-book-upload-card :ref="`bookCard-${book.index}`" :key="index" :book="book" :processing="processing" @remove="removeBook(book)" />
<!-- Item Upload cards -->
<template v-for="(item, index) in items">
<cards-item-upload-card :ref="`itemCard-${item.index}`" :key="index" :media-type="selectedLibraryMediaType" :item="item" :processing="processing" @remove="removeItem(item)" />
</template>
<!-- Upload/Reset btns -->
<div v-show="books.length" class="flex justify-end pb-8 pt-4">
<div v-show="items.length" class="flex justify-end pb-8 pt-4">
<ui-btn v-if="!uploadFinished" color="success" :loading="processing" @click="submit">Upload</ui-btn>
<ui-btn v-else @click="reset">Reset</ui-btn>
</div>
@ -75,7 +78,7 @@ export default {
return {
isDragging: false,
error: '',
books: [],
items: [],
ignoredFiles: [],
selectedLibraryId: null,
selectedFolderId: null,
@ -108,6 +111,12 @@ export default {
selectedLibrary() {
return this.libraries.find((lib) => lib.id === this.selectedLibraryId)
},
selectedLibraryMediaType() {
return this.selectedLibrary ? this.selectedLibrary.mediaType : null
},
selectedLibraryIsPodcast() {
return this.selectedLibraryMediaType === 'podcast'
},
selectedFolder() {
if (!this.selectedLibrary) return null
return this.selectedLibrary.folders.find((fold) => fold.id === this.selectedFolderId)
@ -122,7 +131,7 @@ export default {
})
},
uploadReady() {
return !this.books.length && !this.ignoredFiles.length && !this.uploadFinished
return !this.items.length && !this.ignoredFiles.length && !this.uploadFinished
}
},
methods: {
@ -141,15 +150,15 @@ export default {
this.selectedFolderId = this.selectedLibrary.folders[0].id
}
},
removeBook(book) {
this.books = this.books.filter((b) => b.index !== book.index)
if (!this.books.length) {
removeItem(item) {
this.items = this.items.filter((b) => b.index !== item.index)
if (!this.items.length) {
this.reset()
}
},
reset() {
this.error = ''
this.books = []
this.items = []
this.ignoredFiles = []
this.uploadFinished = false
if (this.$refs.fileInput) this.$refs.fileInput.value = ''
@ -186,31 +195,31 @@ export default {
e.preventDefault()
this.isDragging = false
var items = e.dataTransfer.items || []
var bookResults = await this.uploadHelpers.getBooksFromDrop(items)
this.setResults(bookResults)
var itemResults = await this.uploadHelpers.getItemsFromDrop(items)
this.setResults(itemResults)
},
inputChanged(e) {
if (!e.target || !e.target.files) return
var _files = Array.from(e.target.files)
if (_files && _files.length) {
var bookResults = this.uploadHelpers.getBooksFromPicker(_files)
this.setResults(bookResults)
var itemResults = this.uploadHelpers.getItemsFromPicker(_files, this.selectedLibraryMediaType)
this.setResults(itemResults)
}
},
setResults(bookResults) {
if (bookResults.error) {
this.error = bookResults.error
this.books = []
setResults(itemResults) {
if (itemResults.error) {
this.error = itemResults.error
this.items = []
this.ignoredFiles = []
} else {
this.error = ''
this.books = bookResults.books
this.ignoredFiles = bookResults.ignoredFiles
this.items = itemResults.items
this.ignoredFiles = itemResults.ignoredFiles
}
console.log('Upload results', bookResults)
console.log('Upload results', itemResults)
},
updateBookCardStatus(index, status) {
var ref = this.$refs[`bookCard-${index}`]
updateItemCardStatus(index, status) {
var ref = this.$refs[`itemCard-${index}`]
if (ref && ref.length) ref = ref[0]
if (!ref) {
console.error('Book card ref not found', index, this.$refs)
@ -218,16 +227,18 @@ export default {
ref.setUploadStatus(status)
}
},
uploadBook(book) {
uploadItem(item) {
var form = new FormData()
form.set('title', book.title)
form.set('author', book.author)
form.set('series', book.series)
form.set('title', item.title)
if (!this.selectedLibraryIsPodcast) {
form.set('author', item.author)
form.set('series', item.series)
}
form.set('library', this.selectedLibraryId)
form.set('folder', this.selectedFolderId)
var index = 0
book.files.forEach((file) => {
item.files.forEach((file) => {
form.set(`${index++}`, file)
})
@ -241,24 +252,24 @@ export default {
return false
})
},
validateBooks() {
var bookData = []
for (var book of this.books) {
var bookref = this.$refs[`bookCard-${book.index}`]
if (bookref && bookref.length) bookref = bookref[0]
validateItems() {
var itemData = []
for (var item of this.items) {
var itemref = this.$refs[`itemCard-${item.index}`]
if (itemref && itemref.length) itemref = itemref[0]
if (!bookref) {
console.error('Invalid book index no ref', book.index, this.$refs.bookCard)
if (!itemref) {
console.error('Invalid item index no ref', item.index, this.$refs.itemCard)
return false
} else {
var data = bookref.getData()
var data = itemref.getData()
if (!data) {
return false
}
bookData.push(data)
itemData.push(data)
}
}
return bookData
return itemData
},
async submit() {
if (!this.selectedFolderId || !this.selectedLibraryId) {
@ -267,27 +278,27 @@ export default {
return
}
var books = this.validateBooks()
if (!books) {
this.$toast.error('Some invalid books')
var items = this.validateItems()
if (!items) {
this.$toast.error('Some invalid items')
return
}
this.processing = true
var booksUploaded = 0
var booksFailed = 0
for (let i = 0; i < books.length; i++) {
var book = books[i]
this.updateBookCardStatus(book.index, 'uploading')
var result = await this.uploadBook(book)
if (result) booksUploaded++
else booksFailed++
this.updateBookCardStatus(book.index, result ? 'success' : 'failed')
var itemsUploaded = 0
var itemsFailed = 0
for (let i = 0; i < items.length; i++) {
var item = items[i]
this.updateItemCardStatus(item.index, 'uploading')
var result = await this.uploadItem(item)
if (result) itemsUploaded++
else itemsFailed++
this.updateItemCardStatus(item.index, result ? 'success' : 'failed')
}
if (booksUploaded) {
this.$toast.success(`Successfully uploaded ${booksUploaded} book${booksUploaded > 1 ? 's' : ''}`)
if (itemsUploaded) {
this.$toast.success(`Successfully uploaded ${itemsUploaded} item${itemsUploaded > 1 ? 's' : ''}`)
}
if (booksFailed) {
this.$toast.success(`Failed to upload ${booksFailed} book${booksFailed > 1 ? 's' : ''}`)
if (itemsFailed) {
this.$toast.success(`Failed to upload ${itemsFailed} item${itemsFailed > 1 ? 's' : ''}`)
}
this.processing = false
this.uploadFinished = true

View File

@ -4,7 +4,7 @@ const SupportedFileTypes = {
ebook: ['epub', 'pdf', 'mobi', 'azw3', 'cbr', 'cbz'],
info: ['nfo'],
text: ['txt'],
opf: ['opf']
metadata: ['opf', 'abs']
}
const DownloadStatus = {

View File

@ -1,6 +1,7 @@
const Path = require('path')
const fs = require('fs-extra')
const Logger = require('../Logger')
const filePerms = require('../utils/filePerms')
const { isObject } = require('../utils/index')
@ -37,15 +38,21 @@ class MiscController {
}
// For setting permissions recursively
var firstDirPath = Path.join(folder.fullPath, author)
var outputDirectory = ''
if (series && author) {
outputDirectory = Path.join(folder.fullPath, author, series, title)
} else if (author) {
outputDirectory = Path.join(folder.fullPath, author, title)
} else {
var firstDirPath = ''
if (library.isPodcast) { // Podcasts only in 1 folder
outputDirectory = Path.join(folder.fullPath, title)
firstDirPath = outputDirectory
} else {
firstDirPath = Path.join(folder.fullPath, author)
if (series && author) {
outputDirectory = Path.join(folder.fullPath, author, series, title)
} else if (author) {
outputDirectory = Path.join(folder.fullPath, author, title)
} else {
outputDirectory = Path.join(folder.fullPath, title)
}
}
var exists = await fs.pathExists(outputDirectory)

View File

@ -26,6 +26,9 @@ class Library {
get folderPaths() {
return this.folders.map(f => f.fullPath)
}
get isPodcast() {
return this.mediaType === 'podcast'
}
construct(library) {
this.id = library.id