Skip to content

Commit

Permalink
added useoriginal for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Dec 17, 2024
1 parent 7861a9e commit b6c3c39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
51 changes: 27 additions & 24 deletions packages/nexrender-action-cache/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');

async function findValidateCache(asset, settings, cacheDirectory, ttl){
async function findValidateCache(asset, settings, cacheDirectory, ttl, useOriginal) {
if (asset.src.startsWith('file://')) {
settings.logger.log(`> Skipping cache for ${asset.src}; local file protocol is being used`);
return;
Expand All @@ -28,25 +28,28 @@ async function findValidateCache(asset, settings, cacheDirectory, ttl){
settings.logger.log(`> Cached file found at ${maybeCachedFileLocation}`);
settings.logger.log(`> Old source: ${asset.src}`);
asset.src = `file://${maybeCachedFileLocation}`;
if (useOriginal) asset.useOriginal = true;
settings.logger.log(`> New source: ${asset.src}`);
}

const predownload = async (job, settings, { cacheDirectory, ttl, cacheAssets }) => {
const predownload = async (job, settings, { cacheDirectory, ttl, cacheAssets, useOriginal }) => {
// Job template
await findValidateCache(job.template, settings, cacheDirectory, ttl);

if(cacheAssets){
// Job assets
for(const asset of job.assets){
// Only asset types that can be downloaded files
if(['image', 'audio', 'video', 'script', 'static'].includes(asset.type)){
await findValidateCache(asset, settings, cacheDirectory, ttl);
}
await findValidateCache(job.template, settings, cacheDirectory, ttl, useOriginal);

if (!cacheAssets) {
return;
}

// Job assets
for (const asset of job.assets) {
// Only asset types that can be downloaded files
if (['image', 'audio', 'video', 'script', 'static'].includes(asset.type)) {
await findValidateCache(asset, settings, cacheDirectory, ttl, useOriginal);
}
}
}

async function saveCache(asset, settings, workpath, cacheDirectory){
async function saveCache(asset, settings, workpath, cacheDirectory) {
if (asset.src.startsWith('file://')) {
settings.logger.log(`> Skipping cache for ${asset.src}; local file protocol is being used`);
return;
Expand All @@ -66,21 +69,21 @@ async function saveCache(asset, settings, workpath, cacheDirectory){
}

const postdownload = async (job, settings, { cacheDirectory, cacheAssets }) => {
// Job template
await saveCache(job.template, settings, job.workpath, cacheDirectory);

if(cacheAssets){
// Job assets
for(const asset of job.assets){
// Only asset types that can be downloaded files
if(['image', 'audio', 'video', 'script', 'static'].includes(asset.type)){
await saveCache(asset, settings, job.workpath, cacheDirectory);
}
await saveCache(job.template, settings, job.workpath, cacheDirectory); // Job template

if (!cacheAssets) {
return;
}

// Job assets
for (const asset of job.assets) {
if (['image', 'audio', 'video', 'script', 'static'].includes(asset.type)) {
await saveCache(asset, settings, job.workpath, cacheDirectory);
}
}
}

module.exports = (job, settings, { cacheDirectory, ttl, cacheAssets }, type) => {
module.exports = (job, settings, { cacheDirectory, ttl, cacheAssets, useOriginal = false }, type) => {
if (!cacheDirectory) {
throw new Error(`cacheDirectory not provided.`);
}
Expand All @@ -92,7 +95,7 @@ module.exports = (job, settings, { cacheDirectory, ttl, cacheAssets }, type) =>
}

if (type === 'predownload') {
return predownload(job, settings, { cacheDirectory, ttl, cacheAssets }, type);
return predownload(job, settings, { cacheDirectory, ttl, cacheAssets, useOriginal }, type);
}

if (type === 'postdownload') {
Expand Down
2 changes: 2 additions & 0 deletions packages/nexrender-core/src/tasks/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const NEXRENDER_DOWNLOAD_TIMEOUT = process.env.NEXRENDER_DOWNLOAD_TIMEOUT || 3 *
const download = (job, settings, asset) => {
if (asset.type == 'data') return Promise.resolve();

settings.logger.log(`[${job.uid}] > Downloading asset ${asset.src}...`);

// eslint-disable-next-line
const uri = global.URL ? new URL(asset.src) : url.parse(asset.src)
const protocol = uri.protocol.replace(/:$/, '');
Expand Down

0 comments on commit b6c3c39

Please sign in to comment.