Skip to content

Commit

Permalink
Use org id instead of user id
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed Apr 28, 2024
1 parent 8a75e83 commit c120292
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
8 changes: 8 additions & 0 deletions components/AppFileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const releaseNotes = ref<string | null>(null)
const dialogRef = inject<any>('dialogRef');
const osType = ref<OsType | null>(null)
const prop = ref<any>(null)
const orgName = ref<string>('')
const appName = ref<string>('')
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'))
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions components/Releases.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const upload = () => {
data: {
osType,
props,
orgName: props.orgName,
appName: props.appName,
},
onClose: (o) => {
if (o?.data?.success) {
Expand Down
2 changes: 1 addition & 1 deletion server/api/artifacts/detail-artifact.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion server/api/artifacts/download-artifact.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion server/api/artifacts/upload-artifact-url.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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}`,
Expand Down
21 changes: 20 additions & 1 deletion server/api/artifacts/upload-artifact.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 1 addition & 7 deletions server/api/install/download.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions server/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export const getJwtKey = (event: H3Event<EventHandlerRequest>) => {
)
}

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}`,
}
}

Expand Down

0 comments on commit c120292

Please sign in to comment.