1
0
Fork 0

wip: Work on backend import functionality

This commit is contained in:
Joost De Cock 2023-08-09 17:29:59 +02:00
parent 4ab9f9a9c9
commit ab570b7ebc
3 changed files with 44 additions and 18 deletions

View file

@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken' import jwt from 'jsonwebtoken'
import { log } from '../utils/log.mjs' import { log } from '../utils/log.mjs'
import { hash, hashPassword, randomString, verifyPassword } from '../utils/crypto.mjs' import { hash, hashPassword, randomString, verifyPassword } from '../utils/crypto.mjs'
import { replaceImage } from '../utils/cloudflare-images.mjs' import { replaceImage, ensureImage } from '../utils/cloudflare-images.mjs'
import { clean, asJson, i18nUrl } from '../utils/index.mjs' import { clean, asJson, i18nUrl } from '../utils/index.mjs'
import { ConfirmationModel } from './confirmation.mjs' import { ConfirmationModel } from './confirmation.mjs'
import { SetModel } from './set.mjs' import { SetModel } from './set.mjs'
@ -914,6 +914,7 @@ UserModel.prototype.import = async function (list) {
* Grab the image from the FreeSewing server and upload it to Sanity * Grab the image from the FreeSewing server and upload it to Sanity
*/ */
if (data.img) { if (data.img) {
const imgId = `user-${data.ihash}`
const imgUrl = const imgUrl =
'https://static.freesewing.org/users/' + 'https://static.freesewing.org/users/' +
encodeURIComponent(sub.handle.slice(0, 1)) + encodeURIComponent(sub.handle.slice(0, 1)) +
@ -921,14 +922,16 @@ UserModel.prototype.import = async function (list) {
encodeURIComponent(sub.handle) + encodeURIComponent(sub.handle) +
'/' + '/' +
encodeURIComponent(data.img) encodeURIComponent(data.img)
console.log('Grabbing', imgUrl) data.img = await ensureImage({
//const [contentType, imgData] = await downloadImage(imgUrl) id: imgId,
//// Do not import the default SVG avatar metadata: {
//if (contentType !== 'image/svg+xml') { user: `v2-${sub.handle}`,
// const img = await setUserAvatar(data.ihash, [contentType, imgData], data.username) ihash: data.ihash,
// data.img = img },
//} url: imgUrl,
} })
data.img = imgId
} else data.img = 'default-avatar'
let cloaked = await this.cloak(data) let cloaked = await this.cloak(data)
try { try {
this.record = await this.prisma.user.create({ data: cloaked }) this.record = await this.prisma.user.create({ data: cloaked })
@ -953,8 +956,8 @@ UserModel.prototype.import = async function (list) {
} }
} }
} else skipped.push(sub.email) } else skipped.push(sub.email)
// That's the user, not load their people as sets // That's the user, now load their people as sets
if (sub.people) await this.Set.import(sub, this.record.id) //if (sub.people) await this.Set.import(sub, this.record.id)
} else skipped.push(sub.email) } else skipped.push(sub.email)
} }

View file

@ -60,6 +60,22 @@ export async function replaceImage(props) {
return result.data?.result?.id ? result.data.result.id : false return result.data?.result?.id ? result.data.result.id : false
} }
/*
* Method that uploads an image to cloudflare
* Use this to merely ensure the image exists (will fail silently if it does)
*/
export async function ensureImage(props) {
const form = getFormData(props)
let result
try {
result = await axios.post(config.api, form, { headers })
} catch (err) {
// It's fine
}
return props.id
}
/* /*
* Helper method to construct the form data for cloudflare * Helper method to construct the form data for cloudflare
*/ */

View file

@ -1,6 +1,7 @@
//import subscribers from './v2-newsletters.json' assert { type: 'json' } //import subscribers from './v2-newsletters.json' assert { type: 'json' }
import users from './v2-users.json' assert { type: 'json' } import users from '../dump/v2-users.json' assert { type: 'json' }
import people from './v2-people.json' assert { type: 'json' } import people from '../dump/v2-people.json' assert { type: 'json' }
import patterns from '../dump/v2-patterns.json' assert { type: 'json' }
/* /*
* Only this token allows exporting data * Only this token allows exporting data
@ -64,16 +65,22 @@ const importUsers = async () => {
// Put users in an object with their handle as key // Put users in an object with their handle as key
const allUsers = {} const allUsers = {}
for (const user of todo) allUsers[user.handle] = user for (const user of todo) allUsers[user.handle] = user
// Find all people belonging to these users // Find all people belonging to this user
for (const person of people) { for (const person of people) {
if (typeof allUsers[person.user] !== 'undefined') { if (typeof allUsers[person.user] !== 'undefined') {
if (typeof allUsers[person.user].people === 'undefined') allUsers[person.user].people = [] if (typeof allUsers[person.user].people === 'undefined') allUsers[person.user].people = {}
allUsers[person.user].people.push(person) allUsers[person.user].people[person.handle] = person
}
}
// Find all patterns belonging to this user
for (const pattern of patterns) {
if (typeof allUsers[pattern.user] !== 'undefined') {
if (typeof allUsers[pattern.user].patterns === 'undefined')
allUsers[pattern.user].patterns = {}
allUsers[pattern.user].patterns[pattern.handle] = pattern
} }
} }
console.log('Importing users') console.log('Importing users')
console.log(JSON.stringify(allUsers.joost, null, 2))
process.exit()
const count = todo.length const count = todo.length
let total = 0 let total = 0
const batches = splitArray(todo, 50) const batches = splitArray(todo, 50)