diff --git a/sites/backend/src/models/user.mjs b/sites/backend/src/models/user.mjs index f17e92f3674..16e82d6cf9a 100644 --- a/sites/backend/src/models/user.mjs +++ b/sites/backend/src/models/user.mjs @@ -1,7 +1,7 @@ import jwt from 'jsonwebtoken' import { log } from '../utils/log.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 { ConfirmationModel } from './confirmation.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 */ if (data.img) { + const imgId = `user-${data.ihash}` const imgUrl = 'https://static.freesewing.org/users/' + encodeURIComponent(sub.handle.slice(0, 1)) + @@ -921,14 +922,16 @@ UserModel.prototype.import = async function (list) { encodeURIComponent(sub.handle) + '/' + encodeURIComponent(data.img) - console.log('Grabbing', imgUrl) - //const [contentType, imgData] = await downloadImage(imgUrl) - //// Do not import the default SVG avatar - //if (contentType !== 'image/svg+xml') { - // const img = await setUserAvatar(data.ihash, [contentType, imgData], data.username) - // data.img = img - //} - } + data.img = await ensureImage({ + id: imgId, + metadata: { + user: `v2-${sub.handle}`, + ihash: data.ihash, + }, + url: imgUrl, + }) + data.img = imgId + } else data.img = 'default-avatar' let cloaked = await this.cloak(data) try { this.record = await this.prisma.user.create({ data: cloaked }) @@ -953,8 +956,8 @@ UserModel.prototype.import = async function (list) { } } } else skipped.push(sub.email) - // That's the user, not load their people as sets - if (sub.people) await this.Set.import(sub, this.record.id) + // That's the user, now load their people as sets + //if (sub.people) await this.Set.import(sub, this.record.id) } else skipped.push(sub.email) } diff --git a/sites/backend/src/utils/cloudflare-images.mjs b/sites/backend/src/utils/cloudflare-images.mjs index 2202a3385f8..81e82043fb6 100644 --- a/sites/backend/src/utils/cloudflare-images.mjs +++ b/sites/backend/src/utils/cloudflare-images.mjs @@ -60,6 +60,22 @@ export async function replaceImage(props) { 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 */ diff --git a/sites/backend/v2-v3/import.mjs b/sites/backend/v2-v3/import.mjs index 9ea11284faa..c588c8f396a 100644 --- a/sites/backend/v2-v3/import.mjs +++ b/sites/backend/v2-v3/import.mjs @@ -1,6 +1,7 @@ //import subscribers from './v2-newsletters.json' assert { type: 'json' } -import users from './v2-users.json' assert { type: 'json' } -import people from './v2-people.json' assert { type: 'json' } +import users from '../dump/v2-users.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 @@ -64,16 +65,22 @@ const importUsers = async () => { // Put users in an object with their handle as key const allUsers = {} 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) { if (typeof allUsers[person.user] !== 'undefined') { - if (typeof allUsers[person.user].people === 'undefined') allUsers[person.user].people = [] - allUsers[person.user].people.push(person) + if (typeof allUsers[person.user].people === 'undefined') allUsers[person.user].people = {} + 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(JSON.stringify(allUsers.joost, null, 2)) - process.exit() const count = todo.length let total = 0 const batches = splitArray(todo, 50)