From c574606f0c5dd8d73a256b34ace74497ba0c7b9c Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 5 Mar 2018 23:46:50 -0800 Subject: [PATCH] fix media uploads in restoration script --- bin/restore-mastodon-data.js | 41 ++++++++++++++++++++++++------------ package-lock.json | 28 ++++++++++++++++++++---- package.json | 1 + 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/bin/restore-mastodon-data.js b/bin/restore-mastodon-data.js index b0be5d36..b6c1ad03 100644 --- a/bin/restore-mastodon-data.js +++ b/bin/restore-mastodon-data.js @@ -1,7 +1,6 @@ import { actions } from './mastodon-data' import { users } from '../tests/users' import { pinStatus, postStatus } from '../routes/_api/statuses' -import { uploadMedia } from '../routes/_api/media' import { followAccount } from '../routes/_api/follow' import { favoriteStatus } from '../routes/_api/favorite' import { reblogStatus } from '../routes/_api/reblog' @@ -9,16 +8,39 @@ import fetch from 'node-fetch' import FileApi from 'file-api' import path from 'path' import fs from 'fs' -import pify from 'pify' - -const readFile = pify(fs.readFile.bind(fs)) +import FormData from 'form-data' +import { auth } from '../routes/_api/utils' global.File = FileApi.File global.FormData = FileApi.FormData 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 () { - debugger console.log('Restoring mastodon data...') let internalIdsToIds = {} for (let action of actions) { @@ -30,14 +52,7 @@ export async function restoreMastodonData () { inReplyTo = internalIdsToIds[inReplyTo] } let mediaIds = media && await Promise.all(media.map(async mediaItem => { - let type = mediaItem.endsWith('gif') ? 'image/gif' - : 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') + let mediaResponse = await submitMedia(accessToken, mediaItem, 'kitten') return mediaResponse.id })) let status = await postStatus('localhost:3000', accessToken, text, inReplyTo, mediaIds, diff --git a/package-lock.json b/package-lock.json index 03b01979..1836f766 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3526,13 +3526,23 @@ "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" }, "form-data": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", - "integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz", + "integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=", "requires": { "asynckit": "0.4.0", - "combined-stream": "1.0.5", + "combined-stream": "1.0.6", "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": { @@ -7943,6 +7953,16 @@ "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", "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": { "version": "6.3.2", "resolved": "https://registry.npmjs.org/qs/-/qs-6.3.2.tgz", diff --git a/package.json b/package.json index 3fb8237c..5aa17e64 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "fg-loadcss": "^2.0.1", "file-api": "^0.10.4", "font-awesome-svg-png": "^1.2.2", + "form-data": "^2.3.2", "glob": "^7.1.2", "idb-keyval": "^2.3.0", "indexeddb-getall-shim": "^1.3.1",