Cache ensure dir, cleanup

This commit is contained in:
Dakedres 2024-04-11 08:54:03 -06:00
parent cd3e970b51
commit 6fabb9f858

55
lib.js
View File

@ -47,10 +47,10 @@ export const getLinkExtname = link =>
export const getImageBasePath = (source, postId) =>
`${source.name}-${postId}`
export const writeStylesheet = (path, { directory, batch }) =>
batch.add(
export const writeStylesheet = (path, view) =>
view.batch.add(
FS.readFile(path)
.then(content => write(Path.join(directory, 'style.css'), content))
.then(content => write(Path.join(view.path, 'style.css'), content))
)
export const getPostIdFromPathname = post => {
@ -164,11 +164,25 @@ export const retryDelayedFetch = async (url, options, courtesyWait, retryAttempt
return response
}
export const createCache = async (cache = {}) => {
if(isUnset(cache.enabled)) {
cache.enabled = false
return cache
}
if(isUnset(cache.batch))
cache.batch = new PromiseBatch()
await ensureDir(cache.path)
return cache
}
export const getCacheFilename = (source) =>
source.name + '.xml'
export const getCachePath = (source, cache) =>
Path.join(cache.directory, getCacheFilename(source))
Path.join(cache.path, getCacheFilename(source))
export const cacheSource = (source, cache) =>
write(getCachePath(source, cache), renderCache(source, cache))
@ -338,15 +352,14 @@ export const extractImages = (post) => {
// |/ | |-' |/|/
// ' ' `-' ' '
export const createView = async (directory, pageSize, extra = {}) => {
let view = {
batch: new PromiseBatch(),
directory,
pageSize,
...extra
}
export const createView = async (view = {}) => {
if(isUnset(view.batch))
view.batch = new PromiseBatch()
await ensureDir(view.directory)
if(isUnset(view.header))
view.header = ''
await ensureDir(view.path)
if(view.imageStoreDirectory)
await openImageStore(view)
@ -355,7 +368,7 @@ export const createView = async (directory, pageSize, extra = {}) => {
}
export const openImageStore = async view => {
let imageStorePath = Path.join(view.directory, view.imageStoreDirectory)
let imageStorePath = Path.join(view.path, view.imageStoreDirectory)
view.imageStore = new Map()
if(!await ensureDir(imageStorePath)) {
@ -430,11 +443,11 @@ export const createPages = (list, { pageSize }) => {
return pages
}
export const writePage = (page, { header = '', directory, batch }) => {
let html = renderPage(page.title, page.posts, header, renderNextPageLink(page.lastPageLink))
let promise = write(Path.join(directory, page.filename), html)
export const writePage = (page, view) => {
let html = renderPage(page.title, page.posts, view.header, renderNextPageLink(page.lastPageLink))
let promise = write(Path.join(view.path, page.filename), html)
batch.add(promise.then(annotate(`Created "${page.title}" (${page.filename})`)))
view.batch.add(promise.then(annotate(`Created "${page.title}" (${page.filename})`)))
}
export const getFinalPageFilename = list =>
@ -580,8 +593,8 @@ export const renderNavEntry = (list) => {
// | | | | | | | | | | | | |
// `-' `-' ' ' `-` `-' ' `-' ' '
export const downloadImage = async (url, basename, { courtesyWait, retryAttempts }, { batch, directory, imageStoreDirectory }) => {
let response = await retryDelayedFetch(url, {}, courtesyWait, retryAttempts)
export const downloadImage = async (url, basename, source, view) => {
let response = await retryDelayedFetch(url, {}, source.courtesyWait, source.retryAttempts)
.catch(err => console.error(`Failed download of ${url}:`, err, err.errors) )
if(response == undefined) {
@ -599,13 +612,13 @@ export const downloadImage = async (url, basename, { courtesyWait, retryAttempts
}
let relativePath = basename + extension
let path = Path.join(directory, imageStoreDirectory, relativePath)
let path = Path.join(view.path, view.imageStoreDirectory, relativePath)
const download = () => write(path, response.body)
.then(annotate( `Downloaded ${relativePath}`))
// TODO: See if the image is downloaded before even trying to download it
batch.add(FS.access(path).catch(download))
view.batch.add(FS.access(path).catch(download))
return relativePath
} else {
console.error( createNetworkingError(response) )