Created
June 22, 2021 09:42
-
-
Save cryptoskillz/ce0ab074d625755abf6bc87891c20d32 to your computer and use it in GitHub Desktop.
Fetch the files using the Strapi API so we they can be used in our eleventy static site without having to call back to the API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IN THE ELEVENTY API FILE | |
const superagent = require('superagent'); | |
let env = require('./env') | |
//this function gets some date from and API which can be used in the data processing, for simpluicty stakes it is connection to our auctions endpoint so you can see | |
//it in action | |
let getImages = async () => { | |
try { | |
var res = await superagent.get(env.API_URL+ "/upload/files/").query({}) | |
//check we got a response. | |
if (res.ok) { | |
let images = res.body; | |
for (var i = 0; i < images.length; i++) { | |
//console.log(images[i].url); | |
var http = require('http'), | |
Stream = require('stream').Transform, | |
fs = require('fs'); | |
var url = env.API_URL+images[i].url | |
//console.log(url) | |
//console.log('output/HTML/assets/images'+images[i].url); | |
let imagename = images[i].url; | |
http.request(url, function(response) { | |
var data = new Stream(); | |
response.on('data', function(chunk) { | |
data.push(chunk); | |
}); | |
response.on('end', function() { | |
console.log('hhh output/HTML/assets/images'+imagename ) | |
fs.writeFileSync('output/HTML/assets/images'+imagename , data.read()); | |
}); | |
}).end(); | |
} | |
} | |
} catch (err) { | |
console.log('Error getting something:') | |
console.error(err) | |
} | |
} | |
module.exports = async () => { | |
getImages(); | |
return { | |
} | |
} | |
IN THE TEMPLATE FILE | |
{{contentarray.content | replace("/uploads/", "/assets/images/uploads/") | renderUsingMarkdown | safe}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment