Cache ensure dir, cleanup
This commit is contained in:
parent
cd3e970b51
commit
6fabb9f858
55
lib.js
55
lib.js
@ -47,10 +47,10 @@ export const getLinkExtname = link =>
|
|||||||
export const getImageBasePath = (source, postId) =>
|
export const getImageBasePath = (source, postId) =>
|
||||||
`${source.name}-${postId}`
|
`${source.name}-${postId}`
|
||||||
|
|
||||||
export const writeStylesheet = (path, { directory, batch }) =>
|
export const writeStylesheet = (path, view) =>
|
||||||
batch.add(
|
view.batch.add(
|
||||||
FS.readFile(path)
|
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 => {
|
export const getPostIdFromPathname = post => {
|
||||||
@ -164,11 +164,25 @@ export const retryDelayedFetch = async (url, options, courtesyWait, retryAttempt
|
|||||||
return response
|
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) =>
|
export const getCacheFilename = (source) =>
|
||||||
source.name + '.xml'
|
source.name + '.xml'
|
||||||
|
|
||||||
export const getCachePath = (source, cache) =>
|
export const getCachePath = (source, cache) =>
|
||||||
Path.join(cache.directory, getCacheFilename(source))
|
Path.join(cache.path, getCacheFilename(source))
|
||||||
|
|
||||||
export const cacheSource = (source, cache) =>
|
export const cacheSource = (source, cache) =>
|
||||||
write(getCachePath(source, cache), renderCache(source, cache))
|
write(getCachePath(source, cache), renderCache(source, cache))
|
||||||
@ -338,15 +352,14 @@ export const extractImages = (post) => {
|
|||||||
// |/ | |-' |/|/
|
// |/ | |-' |/|/
|
||||||
// ' ' `-' ' '
|
// ' ' `-' ' '
|
||||||
|
|
||||||
export const createView = async (directory, pageSize, extra = {}) => {
|
export const createView = async (view = {}) => {
|
||||||
let view = {
|
if(isUnset(view.batch))
|
||||||
batch: new PromiseBatch(),
|
view.batch = new PromiseBatch()
|
||||||
directory,
|
|
||||||
pageSize,
|
|
||||||
...extra
|
|
||||||
}
|
|
||||||
|
|
||||||
await ensureDir(view.directory)
|
if(isUnset(view.header))
|
||||||
|
view.header = ''
|
||||||
|
|
||||||
|
await ensureDir(view.path)
|
||||||
|
|
||||||
if(view.imageStoreDirectory)
|
if(view.imageStoreDirectory)
|
||||||
await openImageStore(view)
|
await openImageStore(view)
|
||||||
@ -355,7 +368,7 @@ export const createView = async (directory, pageSize, extra = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const openImageStore = async view => {
|
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()
|
view.imageStore = new Map()
|
||||||
|
|
||||||
if(!await ensureDir(imageStorePath)) {
|
if(!await ensureDir(imageStorePath)) {
|
||||||
@ -430,11 +443,11 @@ export const createPages = (list, { pageSize }) => {
|
|||||||
return pages
|
return pages
|
||||||
}
|
}
|
||||||
|
|
||||||
export const writePage = (page, { header = '', directory, batch }) => {
|
export const writePage = (page, view) => {
|
||||||
let html = renderPage(page.title, page.posts, header, renderNextPageLink(page.lastPageLink))
|
let html = renderPage(page.title, page.posts, view.header, renderNextPageLink(page.lastPageLink))
|
||||||
let promise = write(Path.join(directory, page.filename), html)
|
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 =>
|
export const getFinalPageFilename = list =>
|
||||||
@ -580,8 +593,8 @@ export const renderNavEntry = (list) => {
|
|||||||
// | | | | | | | | | | | | |
|
// | | | | | | | | | | | | |
|
||||||
// `-' `-' ' ' `-` `-' ' `-' ' '
|
// `-' `-' ' ' `-` `-' ' `-' ' '
|
||||||
|
|
||||||
export const downloadImage = async (url, basename, { courtesyWait, retryAttempts }, { batch, directory, imageStoreDirectory }) => {
|
export const downloadImage = async (url, basename, source, view) => {
|
||||||
let response = await retryDelayedFetch(url, {}, courtesyWait, retryAttempts)
|
let response = await retryDelayedFetch(url, {}, source.courtesyWait, source.retryAttempts)
|
||||||
.catch(err => console.error(`Failed download of ${url}:`, err, err.errors) )
|
.catch(err => console.error(`Failed download of ${url}:`, err, err.errors) )
|
||||||
|
|
||||||
if(response == undefined) {
|
if(response == undefined) {
|
||||||
@ -599,13 +612,13 @@ export const downloadImage = async (url, basename, { courtesyWait, retryAttempts
|
|||||||
}
|
}
|
||||||
|
|
||||||
let relativePath = basename + extension
|
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)
|
const download = () => write(path, response.body)
|
||||||
.then(annotate( `Downloaded ${relativePath}`))
|
.then(annotate( `Downloaded ${relativePath}`))
|
||||||
|
|
||||||
// TODO: See if the image is downloaded before even trying to download it
|
// 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
|
return relativePath
|
||||||
} else {
|
} else {
|
||||||
console.error( createNetworkingError(response) )
|
console.error( createNetworkingError(response) )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user