diff --git a/components/AppFileUpload.vue b/components/AppFileUpload.vue index e8aeb0a..ed03271 100644 --- a/components/AppFileUpload.vue +++ b/components/AppFileUpload.vue @@ -14,9 +14,13 @@ const releaseNotes = ref(null) const dialogRef = inject('dialogRef'); const osType = ref(null) const prop = ref(null) +const orgName = ref('') +const appName = ref('') onMounted(() => { osType.value = dialogRef.value.data.osType; prop.value = dialogRef.value.data.props + orgName.value = dialogRef.value.data.orgName + appName.value = dialogRef.value.data.appName }) const mimeTypeFromOsType = computed(() => getMimeTypeFromosType(osType.value ?? 'android')) @@ -50,6 +54,10 @@ const submit = async () => { const onUpload = async (file: File) => { const { url, file: key } = await $fetch('/api/artifacts/upload-artifact', { method: 'post', + body: { + orgName: orgName.value, + appName: appName.value, + }, }) await $fetch(url, { method: 'put', diff --git a/components/Releases.vue b/components/Releases.vue index 8c8d8d3..5de890f 100644 --- a/components/Releases.vue +++ b/components/Releases.vue @@ -58,6 +58,8 @@ const upload = () => { data: { osType, props, + orgName: props.orgName, + appName: props.appName, }, onClose: (o) => { if (o?.data?.success) { diff --git a/server/api/artifacts/detail-artifact.get.ts b/server/api/artifacts/detail-artifact.get.ts index eb6d4bc..dca7da4 100644 --- a/server/api/artifacts/detail-artifact.get.ts +++ b/server/api/artifacts/detail-artifact.get.ts @@ -31,7 +31,7 @@ export default defineEventHandler(async (event) => { }, }).then(takeUniqueOrThrow) const s3 = createS3(event) - const { assets } = getStorageKeys(event.context.auth, detailArtifact.fileObjectKey) + const { assets } = getStorageKeys(userOrg.organizationsId!, app.id, detailArtifact.fileObjectKey) const [headObject, groups] = await Promise.all([ s3.send(new HeadObjectCommand({ Bucket: s3BucketName, diff --git a/server/api/artifacts/download-artifact.get.ts b/server/api/artifacts/download-artifact.get.ts index 3cffb29..6b4181b 100644 --- a/server/api/artifacts/download-artifact.get.ts +++ b/server/api/artifacts/download-artifact.get.ts @@ -31,7 +31,7 @@ export default defineEventHandler(async (event) => { ) }, }).then(takeUniqueOrThrow) - const { assets } = getStorageKeys(event.context.auth, detailArtifact.fileObjectKey) + const { assets } = getStorageKeys(userOrg.organizationsId!, app.id, detailArtifact.fileObjectKey) const s3 = createS3(event) const signedUrl = await getSignedUrl(s3, new GetObjectCommand({ Bucket: s3BucketName, diff --git a/server/api/artifacts/upload-artifact-url.post.ts b/server/api/artifacts/upload-artifact-url.post.ts index 30bb110..710f588 100644 --- a/server/api/artifacts/upload-artifact-url.post.ts +++ b/server/api/artifacts/upload-artifact-url.post.ts @@ -7,7 +7,6 @@ import { CopyObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3" export default defineEventHandler(async (event) => { const { key, appName, orgName, releaseNotes } = await readBody(event) - const { temp, assets } = getStorageKeys(event.context.auth, key) const userId = event.context.auth.userId const db = event.context.drizzle const userOrg = await db.select({ @@ -44,6 +43,7 @@ export default defineEventHandler(async (event) => { releaseNotes: releaseNotes, releaseId: newReleaseId, }) + const { temp, assets } = getStorageKeys(userOrg.organizationsId!, app.id, key) const s3 = createS3(event) await s3.send(new CopyObjectCommand({ CopySource: `${s3BucketName}/${temp}`, diff --git a/server/api/artifacts/upload-artifact.post.ts b/server/api/artifacts/upload-artifact.post.ts index 7c26fc3..0369119 100644 --- a/server/api/artifacts/upload-artifact.post.ts +++ b/server/api/artifacts/upload-artifact.post.ts @@ -3,11 +3,30 @@ import { generateRandomPassword, getStorageKeys, s3BucketName } from "~/server/u import { createS3O } from "~/server/services/s3" import { getSignedUrl } from "@aws-sdk/s3-request-presigner" import { PutObjectCommand } from "@aws-sdk/client-s3"; +import { organizations, organizationsPeople } from "~/server/db/schema"; +import { and, eq } from "drizzle-orm"; +import { takeUniqueOrThrow } from "../detail-app.get"; export default defineEventHandler(async (event) => { + const { orgName, appName } = await readBody(event) + const userId = event.context.auth.userId + const db = event.context.drizzle + const userOrg = await db.select({ + organizationsId: organizations.id, + }) + .from(organizationsPeople) + .leftJoin(organizations, eq(organizations.id, organizationsPeople.organizationId)) + .where(and(eq(organizationsPeople.userId, userId), eq(organizations.name, orgName!.toString()))) + .then(takeUniqueOrThrow) + const app = await db.query.apps.findMany({ + where(fields, operators) { + return operators.and(operators.eq(fields.organizationsId, userOrg.organizationsId!), operators.eq(fields.name, appName!.toString())) + }, + }).then(takeUniqueOrThrow) + const key = generateRandomPassword() var expires = 300; - const { temp } = getStorageKeys(event.context.auth, key) + const { temp } = getStorageKeys(userOrg.organizationsId!, app.id, key) const s3 = createS3O(event) const signedUrl = await getSignedUrl(s3, new PutObjectCommand({ Bucket: s3BucketName, diff --git a/server/api/install/download.get.ts b/server/api/install/download.get.ts index 24afbeb..6c38c2a 100644 --- a/server/api/install/download.get.ts +++ b/server/api/install/download.get.ts @@ -32,12 +32,6 @@ export default defineEventHandler(async (event) => { return operators.eq(fields.id, app.organizationsId!) }, }).then(takeUniqueOrThrow) - const orgPeople = await db.query.organizationsPeople.findMany({ - where(fields, operators) { - return operators.eq(fields.organizationId, org.id) - }, - limit: 1, - }).then(takeUniqueOrThrow) const detailArtifact = await db.query.artifacts.findMany({ where(fields, operators) { return operators.and( @@ -46,7 +40,7 @@ export default defineEventHandler(async (event) => { ) }, }).then(takeUniqueOrThrow) - const { assets } = getStorageKeys(orgPeople.userId!, detailArtifact.fileObjectKey) + const { assets } = getStorageKeys(org.id, app.id, detailArtifact.fileObjectKey) const s3 = createS3(event) const signedUrl = await getSignedUrl(s3, new GetObjectCommand({ Bucket: s3BucketName, diff --git a/server/utils/utils.ts b/server/utils/utils.ts index b6d1d38..760da08 100644 --- a/server/utils/utils.ts +++ b/server/utils/utils.ts @@ -14,11 +14,10 @@ export const getJwtKey = (event: H3Event) => { ) } -export const getStorageKeys = (auth: AuthData | string, key: String) => { - const userId = typeof auth === 'string' ? auth : auth.userId +export const getStorageKeys = (orgId: string, appId: string, key: string) => { return { - temp: `temporary/userId-${userId}/${key}`, - assets: `assets/userId-${userId}/${key}`, + temp: `temporary/orgid-${orgId}-${appId}/${key}`, + assets: `assets/orgid-${orgId}-${appId}/${key}`, } }