Find mimeType/update image.url

This commit is contained in:
mattwr18 2020-03-16 19:20:02 +01:00
parent 179d9d000a
commit c33217c526
3 changed files with 18 additions and 9 deletions

View File

@ -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",

View File

@ -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) {

View File

@ -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==