fix media uploads in restoration script

This commit is contained in:
Nolan Lawson 2018-03-05 23:46:50 -08:00
parent 8eeb7da186
commit c574606f0c
3 changed files with 53 additions and 17 deletions

View File

@ -1,7 +1,6 @@
import { actions } from './mastodon-data' import { actions } from './mastodon-data'
import { users } from '../tests/users' import { users } from '../tests/users'
import { pinStatus, postStatus } from '../routes/_api/statuses' import { pinStatus, postStatus } from '../routes/_api/statuses'
import { uploadMedia } from '../routes/_api/media'
import { followAccount } from '../routes/_api/follow' import { followAccount } from '../routes/_api/follow'
import { favoriteStatus } from '../routes/_api/favorite' import { favoriteStatus } from '../routes/_api/favorite'
import { reblogStatus } from '../routes/_api/reblog' import { reblogStatus } from '../routes/_api/reblog'
@ -9,16 +8,39 @@ import fetch from 'node-fetch'
import FileApi from 'file-api' import FileApi from 'file-api'
import path from 'path' import path from 'path'
import fs from 'fs' import fs from 'fs'
import pify from 'pify' import FormData from 'form-data'
import { auth } from '../routes/_api/utils'
const readFile = pify(fs.readFile.bind(fs))
global.File = FileApi.File global.File = FileApi.File
global.FormData = FileApi.FormData global.FormData = FileApi.FormData
global.fetch = fetch global.fetch = fetch
async function submitMedia(accessToken, filename, alt) {
let form = new FormData()
form.append('file', fs.createReadStream(path.join(__dirname, '../tests/images/' + filename)))
form.append('description', alt)
return new Promise((resolve, reject) => {
form.submit({
host: 'localhost',
port: 3000,
path: '/api/v1/media',
headers: auth(accessToken)
}, (err, res) => {
if (err) {
return reject(err)
}
let data = ''
res.on('data', chunk => {
data += chunk
})
res.on('end', () => resolve(JSON.parse(data)))
})
})
}
export async function restoreMastodonData () { export async function restoreMastodonData () {
debugger
console.log('Restoring mastodon data...') console.log('Restoring mastodon data...')
let internalIdsToIds = {} let internalIdsToIds = {}
for (let action of actions) { for (let action of actions) {
@ -30,14 +52,7 @@ export async function restoreMastodonData () {
inReplyTo = internalIdsToIds[inReplyTo] inReplyTo = internalIdsToIds[inReplyTo]
} }
let mediaIds = media && await Promise.all(media.map(async mediaItem => { let mediaIds = media && await Promise.all(media.map(async mediaItem => {
let type = mediaItem.endsWith('gif') ? 'image/gif' let mediaResponse = await submitMedia(accessToken, mediaItem, 'kitten')
: mediaItem.endsWith('jpg') ? 'image/jpg' : 'video/mp4'
let file = new File({
name: mediaItem,
type: type,
buffer: await readFile(path.join(__dirname, '../tests/images/' + mediaItem))
})
let mediaResponse = await uploadMedia('localhost:3000', accessToken, file, 'kitten')
return mediaResponse.id return mediaResponse.id
})) }))
let status = await postStatus('localhost:3000', accessToken, text, inReplyTo, mediaIds, let status = await postStatus('localhost:3000', accessToken, text, inReplyTo, mediaIds,

28
package-lock.json generated
View File

@ -3526,13 +3526,23 @@
"integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE="
}, },
"form-data": { "form-data": {
"version": "2.1.4", "version": "2.3.2",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz",
"integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=", "integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=",
"requires": { "requires": {
"asynckit": "0.4.0", "asynckit": "0.4.0",
"combined-stream": "1.0.5", "combined-stream": "1.0.6",
"mime-types": "2.1.17" "mime-types": "2.1.17"
},
"dependencies": {
"combined-stream": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz",
"integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=",
"requires": {
"delayed-stream": "1.0.0"
}
}
} }
}, },
"formdata": { "formdata": {
@ -7943,6 +7953,16 @@
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz",
"integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=" "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ="
}, },
"form-data": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz",
"integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=",
"requires": {
"asynckit": "0.4.0",
"combined-stream": "1.0.5",
"mime-types": "2.1.17"
}
},
"qs": { "qs": {
"version": "6.3.2", "version": "6.3.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.3.2.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.3.2.tgz",

View File

@ -37,6 +37,7 @@
"fg-loadcss": "^2.0.1", "fg-loadcss": "^2.0.1",
"file-api": "^0.10.4", "file-api": "^0.10.4",
"font-awesome-svg-png": "^1.2.2", "font-awesome-svg-png": "^1.2.2",
"form-data": "^2.3.2",
"glob": "^7.1.2", "glob": "^7.1.2",
"idb-keyval": "^2.3.0", "idb-keyval": "^2.3.0",
"indexeddb-getall-shim": "^1.3.1", "indexeddb-getall-shim": "^1.3.1",