Skip to content

Commit

Permalink
add upload images
Browse files Browse the repository at this point in the history
  • Loading branch information
KlonD90 committed Jul 12, 2023
1 parent c3c62b4 commit c02e4fc
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 7 deletions.
37 changes: 30 additions & 7 deletions scripts/import_old_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ var path = require('path');
const cheerio = require('cheerio');
const axios = require('axios')
const qs = require('qs')
const sanitizeHtml = require('sanitize-html');
const mime = require('mime');

const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}

var siteDir = path.join(__dirname, 'blog/_site/');
const client = axios.create({
Expand Down Expand Up @@ -46,6 +45,17 @@ const processPosts = async (posts) => {
}

const uploadPost = async (title, date, tags, author, slug, body) => {
const $ = cheerio.load(body, {
xmlMode: false,
decodeEntities: false,
})
const images = $('img')
for (const img of images) {
const src = $(img).attr('src')
const newSrc = await uploadImage(src)
$(img).attr('src', newSrc)
}
const content = $('body').html()
const splittedTags = tags.split(' ').map(x => x.trim()).filter(x => x.length > 0)
const tagIds = []
for (const tag of splittedTags) {
Expand All @@ -58,13 +68,25 @@ const uploadPost = async (title, date, tags, author, slug, body) => {
date,
author,
slug,
content: body,
content,
tags: tagIds
}
})

}
const uploadImage = async (src) => {
const file = fs.readFileSync(path.join(__dirname, 'blog/_site/', src.slice(1)))
const form = new FormData();

const name = src.split('/').slice(-1)[0]
const ext = name.split('.').slice(-1)[0]
const type = mime.getType(ext)

form.append('files', new Blob([file], { type }), name);
const res = await client.post('/api/upload', form)
const data = res.data[0]
return data.url
}
const readMd = (fileName) => {
const postsDir = path.join(__dirname, 'blog/_posts/');
const dirNames = fileName.split('/')
Expand Down Expand Up @@ -104,14 +126,15 @@ for (const year of yearDirs) {
const tags = md.match(/tags: (.*)/)[1]
const author = md.match(/author: (.*)/)[1]
const slug = file.split('.')[0]
console.log('slug', slug)
addContent.push({
title,
title: sanitizeHtml(title, {
allowedTags: [],
}),
date,
tags,
author,
slug,
body
body,
})
}
}
Expand Down
180 changes: 180 additions & 0 deletions scripts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"dependencies": {
"axios": "^1.4.0",
"cheerio": "^1.0.0-rc.12",
"mime": "^3.0.0",
"sanitize-html": "^2.11.0",
"strapi": "^3.6.11"
}
}

0 comments on commit c02e4fc

Please sign in to comment.