Add media and checkSHA to fetch-content
This commit is contained in:
parent
282ca4400f
commit
0e03e256d0
1 changed files with 48 additions and 6 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
const matter = require('gray-matter')
|
const matter = require('gray-matter')
|
||||||
const yaml = require('js-yaml')
|
const yaml = require('js-yaml')
|
||||||
|
|
||||||
|
const mediaFolder = 'public/images/uploads'
|
||||||
|
const publicFolder = '/images/uploads'
|
||||||
|
|
||||||
const b64DecodeUnicode = str =>
|
const b64DecodeUnicode = str =>
|
||||||
// https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings#30106551
|
// https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings#30106551
|
||||||
decodeURIComponent(
|
decodeURIComponent(
|
||||||
|
|
@ -29,6 +32,13 @@ const parseYaml = data => {
|
||||||
return yaml.safeLoad(data) || {}
|
return yaml.safeLoad(data) || {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const replaceUploadUrls = (uploads = [], string) => {
|
||||||
|
uploads.forEach(upload => {
|
||||||
|
string = string.replace(upload.publicPath, upload.download_url)
|
||||||
|
})
|
||||||
|
return string
|
||||||
|
}
|
||||||
|
|
||||||
export const fetchContent = async (rateLimit = 0) => {
|
export const fetchContent = async (rateLimit = 0) => {
|
||||||
if (!window.localStorage || !window.netlifyIdentity) {
|
if (!window.localStorage || !window.netlifyIdentity) {
|
||||||
return Promise.resolve(null)
|
return Promise.resolve(null)
|
||||||
|
|
@ -45,13 +55,13 @@ export const fetchContent = async (rateLimit = 0) => {
|
||||||
setTimeout(() => resolve(), time)
|
setTimeout(() => resolve(), time)
|
||||||
})
|
})
|
||||||
|
|
||||||
const fetchGithub = (path = 'content') => {
|
const fetchGithub = (path = 'content', endpoint = 'contents') => {
|
||||||
const siteUrl = window.localStorage.netlifySiteURL || ''
|
const siteUrl = window.localStorage.netlifySiteURL || ''
|
||||||
const endpoint = `${siteUrl}/.netlify/git/github/contents/${path}`
|
const url = `${siteUrl}/.netlify/git/github/${endpoint}/${path}`
|
||||||
console.log(`Fetching ${path}`)
|
console.log(`Fetching ${path}`)
|
||||||
return wait(rateLimit)
|
return wait(rateLimit)
|
||||||
.then(() =>
|
.then(() =>
|
||||||
fetch(endpoint, {
|
fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`
|
Authorization: `Bearer ${token}`
|
||||||
},
|
},
|
||||||
|
|
@ -66,8 +76,39 @@ export const fetchContent = async (rateLimit = 0) => {
|
||||||
.catch(console.error)
|
.catch(console.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const checkSHA = () => {
|
||||||
|
console.log('Checking SHA')
|
||||||
|
let githubSHA = ''
|
||||||
|
let localSHA = ''
|
||||||
|
return fetchGithub('refs/heads/master', 'git')
|
||||||
|
.then(ref => {
|
||||||
|
githubSHA = ref.object.sha
|
||||||
|
})
|
||||||
|
.then(() => fetch('/sha'))
|
||||||
|
.then(res => res.text())
|
||||||
|
.then(res => (localSHA = res))
|
||||||
|
.then(() => {
|
||||||
|
if (githubSHA.trim() === localSHA.trim()) {
|
||||||
|
return Promise.reject('Github latest commit equal to build')
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`Github SHA ${githubSHA} not equal to build SHA ${localSHA}: Fetching content from git`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let data = {}
|
let data = {}
|
||||||
return fetchGithub()
|
let uploads = []
|
||||||
|
return checkSHA()
|
||||||
|
.then(() => fetchGithub(mediaFolder))
|
||||||
|
.then((res = []) => {
|
||||||
|
uploads = res.filter(file => file.type !== 'dir').map(file => ({
|
||||||
|
...file,
|
||||||
|
publicPath: `${publicFolder}/${file.name}`
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
.then(() => fetchGithub())
|
||||||
.then(items => {
|
.then(items => {
|
||||||
if (!items) throw new Error('No items found')
|
if (!items) throw new Error('No items found')
|
||||||
const dirs = items.filter(item => item.type === 'dir')
|
const dirs = items.filter(item => item.type === 'dir')
|
||||||
|
|
@ -87,7 +128,8 @@ export const fetchContent = async (rateLimit = 0) => {
|
||||||
.then(files => {
|
.then(files => {
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const fileType = getFileExtension(file.name)
|
const fileType = getFileExtension(file.name)
|
||||||
const fileContents = b64DecodeUnicode(file.content)
|
let fileContents = b64DecodeUnicode(file.content)
|
||||||
|
fileContents = replaceUploadUrls(uploads, fileContents)
|
||||||
const json =
|
const json =
|
||||||
fileType === 'md'
|
fileType === 'md'
|
||||||
? parseMarkdown(fileContents)
|
? parseMarkdown(fileContents)
|
||||||
|
|
@ -100,5 +142,5 @@ export const fetchContent = async (rateLimit = 0) => {
|
||||||
return Promise.all(dirsToFetch)
|
return Promise.all(dirsToFetch)
|
||||||
})
|
})
|
||||||
.then(() => data)
|
.then(() => data)
|
||||||
.catch(console.error)
|
.catch(console.warn)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue