mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Ensure overwritten images are deleted on S3
This commit is contained in:
parent
2447e2b39b
commit
eb838257d4
@ -22,3 +22,4 @@ AWS_ACCESS_KEY_ID=
|
|||||||
AWS_SECRET_ACCESS_KEY=
|
AWS_SECRET_ACCESS_KEY=
|
||||||
AWS_ENDPOINT=
|
AWS_ENDPOINT=
|
||||||
AWS_REGION=
|
AWS_REGION=
|
||||||
|
AWS_BUCKET=
|
||||||
|
|||||||
@ -22,7 +22,7 @@ const {
|
|||||||
AWS_SECRET_ACCESS_KEY,
|
AWS_SECRET_ACCESS_KEY,
|
||||||
AWS_ENDPOINT,
|
AWS_ENDPOINT,
|
||||||
AWS_REGION,
|
AWS_REGION,
|
||||||
AWS_BUCKET = 'image-upload',
|
AWS_BUCKET,
|
||||||
NEO4J_URI = 'bolt://localhost:7687',
|
NEO4J_URI = 'bolt://localhost:7687',
|
||||||
NEO4J_USERNAME = 'neo4j',
|
NEO4J_USERNAME = 'neo4j',
|
||||||
NEO4J_PASSWORD = 'neo4j',
|
NEO4J_PASSWORD = 'neo4j',
|
||||||
@ -71,12 +71,16 @@ export const developmentConfigs = {
|
|||||||
export const sentryConfigs = { SENTRY_DSN_BACKEND, COMMIT }
|
export const sentryConfigs = { SENTRY_DSN_BACKEND, COMMIT }
|
||||||
export const redisConfigs = { REDIS_DOMAIN, REDIS_PORT, REDIS_PASSWORD }
|
export const redisConfigs = { REDIS_DOMAIN, REDIS_PORT, REDIS_PASSWORD }
|
||||||
|
|
||||||
|
const S3_CONFIGURED =
|
||||||
|
AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY && AWS_ENDPOINT && AWS_REGION && AWS_BUCKET
|
||||||
|
|
||||||
export const s3Configs = {
|
export const s3Configs = {
|
||||||
AWS_ACCESS_KEY_ID,
|
AWS_ACCESS_KEY_ID,
|
||||||
AWS_SECRET_ACCESS_KEY,
|
AWS_SECRET_ACCESS_KEY,
|
||||||
AWS_ENDPOINT,
|
AWS_ENDPOINT,
|
||||||
AWS_REGION,
|
AWS_REGION,
|
||||||
AWS_BUCKET,
|
AWS_BUCKET,
|
||||||
|
S3_CONFIGURED,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@ -21,9 +21,10 @@ export async function up(next) {
|
|||||||
AWS_ENDPOINT: endpoint,
|
AWS_ENDPOINT: endpoint,
|
||||||
AWS_REGION: region,
|
AWS_REGION: region,
|
||||||
AWS_BUCKET: Bucket,
|
AWS_BUCKET: Bucket,
|
||||||
|
S3_CONFIGURED,
|
||||||
} = s3Configs
|
} = s3Configs
|
||||||
|
|
||||||
if (!(accessKeyId || secretAccessKey)) {
|
if (!S3_CONFIGURED) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('No S3 given, cannot upload image files')
|
console.log('No S3 given, cannot upload image files')
|
||||||
return
|
return
|
||||||
|
|||||||
@ -14,6 +14,7 @@ const {
|
|||||||
AWS_ENDPOINT: endpoint,
|
AWS_ENDPOINT: endpoint,
|
||||||
AWS_REGION: region,
|
AWS_REGION: region,
|
||||||
AWS_BUCKET: Bucket,
|
AWS_BUCKET: Bucket,
|
||||||
|
S3_CONFIGURED,
|
||||||
} = s3Configs
|
} = s3Configs
|
||||||
|
|
||||||
export async function deleteImage(resource, relationshipType, opts = {}) {
|
export async function deleteImage(resource, relationshipType, opts = {}) {
|
||||||
@ -88,7 +89,10 @@ const wrapTransaction = async (wrappedCallback, args, opts) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteImageFile = (image, deleteCallback = localFileDelete) => {
|
const deleteImageFile = (image, deleteCallback) => {
|
||||||
|
if (!deleteCallback) {
|
||||||
|
deleteCallback = S3_CONFIGURED ? s3Delete : localFileDelete
|
||||||
|
}
|
||||||
const { url } = image
|
const { url } = image
|
||||||
deleteCallback(url)
|
deleteCallback(url)
|
||||||
return url
|
return url
|
||||||
@ -96,7 +100,7 @@ const deleteImageFile = (image, deleteCallback = localFileDelete) => {
|
|||||||
|
|
||||||
const uploadImageFile = async (upload, uploadCallback) => {
|
const uploadImageFile = async (upload, uploadCallback) => {
|
||||||
if (!uploadCallback) {
|
if (!uploadCallback) {
|
||||||
uploadCallback = AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY ? s3Upload : localFileUpload
|
uploadCallback = S3_CONFIGURED ? s3Upload : localFileUpload
|
||||||
}
|
}
|
||||||
const { createReadStream, filename, mimetype } = await upload
|
const { createReadStream, filename, mimetype } = await upload
|
||||||
const { name, ext } = path.parse(filename)
|
const { name, ext } = path.parse(filename)
|
||||||
@ -123,7 +127,7 @@ const localFileUpload = ({ createReadStream, destination }) => {
|
|||||||
|
|
||||||
const s3Upload = async ({ createReadStream, uniqueFilename, mimetype }) => {
|
const s3Upload = async ({ createReadStream, uniqueFilename, mimetype }) => {
|
||||||
const s3 = new S3({ region, endpoint })
|
const s3 = new S3({ region, endpoint })
|
||||||
const s3Location = `original${uniqueFilename}`
|
const s3Location = `original/${uniqueFilename}`
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
Bucket,
|
Bucket,
|
||||||
@ -141,3 +145,14 @@ const localFileDelete = async url => {
|
|||||||
const location = `public${url}`
|
const location = `public${url}`
|
||||||
if (existsSync(location)) unlinkSync(location)
|
if (existsSync(location)) unlinkSync(location)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const s3Delete = async url => {
|
||||||
|
const s3 = new S3({ region, endpoint })
|
||||||
|
let { pathname } = new URL(url)
|
||||||
|
pathname = pathname.substring(1) // remove first character '/'
|
||||||
|
const params = {
|
||||||
|
Bucket,
|
||||||
|
Key: pathname,
|
||||||
|
}
|
||||||
|
await s3.deleteObject(params).promise()
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user