diff --git a/backend/package.json b/backend/package.json index d63705903..9a36d6cae 100644 --- a/backend/package.json +++ b/backend/package.json @@ -87,6 +87,7 @@ "metascraper-video": "^5.11.6", "metascraper-youtube": "^5.11.6", "migrate": "^1.6.2", + "mime-types": "^2.1.26", "minimatch": "^3.0.4", "mustache": "^4.0.1", "neo4j-driver": "^4.0.2", diff --git a/backend/src/db/migrations/20200312140328-bulk_upload_to_s3.js b/backend/src/db/migrations/20200312140328-bulk_upload_to_s3.js index 97a9c01d0..1ebc55daa 100644 --- a/backend/src/db/migrations/20200312140328-bulk_upload_to_s3.js +++ b/backend/src/db/migrations/20200312140328-bulk_upload_to_s3.js @@ -2,6 +2,7 @@ import { getDriver } from '../../db/neo4j' import { createReadStream } from 'fs' import path from 'path' import { S3 } from 'aws-sdk' +import mime from 'mime-types' import { s3Configs } from '../../config' export const description = ` @@ -29,7 +30,6 @@ export async function up(next) { } const s3 = new S3({ region, endpoint }) - try { // Implement your migration here. const { records } = await transaction.run('MATCH (image:Image) RETURN image.url as url') @@ -41,26 +41,34 @@ export async function up(next) { return async () => { const { pathname } = new URL(url, 'http://example.org') const fileLocation = path.join(__dirname, `../../../public/${pathname}`) - const s3Location = `/original/${pathname}` + const s3Location = `original${pathname}` + const mimeType = mime.lookup(fileLocation) const params = { Bucket, Key: s3Location, ACL: 'public-read', - // TODO: check the actual mime type - ContentType: 'image/jpg', + ContentType: mimeType, Body: createReadStream(fileLocation), } + const data = await s3.upload(params).promise() - const { Location } = data - return Location + const { Location: spacesUrl } = data + + const updatedRecord = await transaction.run( + 'MATCH (image:Image {url: $url}) SET image.url = $spacesUrl RETURN image.url as url', + { url, spacesUrl }, + ) + const [updatedUrl] = updatedRecord.records.map(record => record.get('url')) + // eslint-disable-next-line no-console + // https://image-upload.fra1.digitaloceanspaces.com/original/uploads/05b6cb85-deec-45f2-8e34-44111dceb743-avatar.png + return updatedUrl } }) .map(p => p()), ) - // TODO: update the urls in the database // eslint-disable-next-line no-console - console.log(locations) + console.log('this is locations', locations) await transaction.commit() next() } catch (error) { diff --git a/backend/yarn.lock b/backend/yarn.lock index 9e60cd776..77b487fb7 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -6436,7 +6436,7 @@ mime-db@1.43.0: resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.43.0.tgz#0a12e0502650e473d735535050e7c8f4eb4fae58" integrity sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ== -mime-types@^2.1.12, mime-types@~2.1.19, mime-types@~2.1.22, mime-types@~2.1.24, mime-types@~2.1.26: +mime-types@^2.1.12, mime-types@^2.1.26, mime-types@~2.1.19, mime-types@~2.1.22, mime-types@~2.1.24, mime-types@~2.1.26: version "2.1.26" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.26.tgz#9c921fc09b7e149a65dfdc0da4d20997200b0a06" integrity sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==