'Image stores' to enable better image caching
This commit is contained in:
parent
9a587e4391
commit
448fd6dc30
56
lib.js
56
lib.js
@ -46,7 +46,7 @@ export const getLinkExtname = link =>
|
||||
Path.extname(new URL(link).pathname)
|
||||
|
||||
export const getImageBasePath = (source, postId) =>
|
||||
`images/${source.name}-${postId}`
|
||||
`${source.name}-${postId}`
|
||||
|
||||
export const writeStylesheet = (path, { directory, batch }) =>
|
||||
batch.add(
|
||||
@ -66,12 +66,29 @@ export const testBlacklist = (array, blacklist) =>
|
||||
blacklist.find(tag => array.includes(tag)) !== undefined
|
||||
|
||||
export const createView = (directory, pageSize, extra = {}) => {
|
||||
return {
|
||||
let view = {
|
||||
batch: new PromiseBatch(),
|
||||
directory,
|
||||
pageSize,
|
||||
...extra
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
export const openImageStore = async view => {
|
||||
let imageStorePath = Path.join(view.directory, view.imageStoreDirectory)
|
||||
let dirents = await FS.readdir(imageStorePath, { withFileTypes: true })
|
||||
view.imageStore = new Map()
|
||||
|
||||
for(let dirent of dirents) {
|
||||
if(dirent.isFile()) {
|
||||
let basename = dirent.name.slice(0, dirent.name.lastIndexOf('.'))
|
||||
view.imageStore.set(basename, Path.join(view.imageStoreDirectory, dirent.name))
|
||||
}
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
export const isUnset = (value) => {
|
||||
@ -251,14 +268,22 @@ export const createPosts = async (channel, source, fromDate, reducerCallback) =>
|
||||
|
||||
source.items.push(item)
|
||||
|
||||
let promise = reducerCallback(post)
|
||||
.then(post => {
|
||||
if(post) {
|
||||
source.posts.push(post)
|
||||
}
|
||||
})
|
||||
let postResolvable = reducerCallback(post)
|
||||
|
||||
if(postResolvable instanceof Promise) {
|
||||
postResolvable
|
||||
.then(post => {
|
||||
if(post) {
|
||||
source.posts.push(post)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
if(postResolvable) {
|
||||
source.posts.push(postResolvable)
|
||||
}
|
||||
}
|
||||
|
||||
promises.push(promise)
|
||||
promises.push(postResolvable)
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
@ -513,7 +538,7 @@ export const renderNavEntry = (list) => {
|
||||
// | | | | | | | | | | | | |
|
||||
// `-' `-' ' ' `-` `-' ' `-' ' '
|
||||
|
||||
export const downloadImage = async (url, basename, { courtesyWait, retryAttempts }, { batch, directory }) => {
|
||||
export const downloadImage = async (url, basename, { courtesyWait, retryAttempts }, { batch, directory, imageStoreDirectory }) => {
|
||||
let response = await retryDelayedFetch(url, {}, courtesyWait, retryAttempts)
|
||||
.catch(err => console.error(`Failed download of ${url}:`, err, err.errors) )
|
||||
|
||||
@ -532,7 +557,7 @@ export const downloadImage = async (url, basename, { courtesyWait, retryAttempts
|
||||
}
|
||||
|
||||
let relativePath = basename + extension
|
||||
let path = Path.join(directory, relativePath)
|
||||
let path = Path.join(directory, imageStoreDirectory, relativePath)
|
||||
|
||||
const download = () => write(path, response.body)
|
||||
.then(annotate( `Downloaded ${relativePath}`))
|
||||
@ -552,8 +577,13 @@ export const downloadImages = (images, source, postId, view) => {
|
||||
|
||||
for(let i = 0; i < images.length; i++) {
|
||||
let basename = images.length > 1 ? basePath + '-' + i : basePath
|
||||
let pathname = view.imageStore.get(basename)
|
||||
|
||||
pathnames.push(downloadImage(images[i], basename, source, view))
|
||||
if(isUnset(pathname)) {
|
||||
pathname = downloadImage(images[i], basename, source, view)
|
||||
}
|
||||
|
||||
pathnames.push(pathname)
|
||||
}
|
||||
|
||||
return Promise.all(pathnames)
|
||||
@ -827,4 +857,4 @@ export const mastodon = {
|
||||
return post
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user