Skip to content

Commit

Permalink
Add edge case upload_temp to cleanup stale upload
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed Dec 8, 2024
1 parent 5c07051 commit c8e692d
Show file tree
Hide file tree
Showing 12 changed files with 2,921 additions and 20 deletions.
8 changes: 5 additions & 3 deletions components/AppFileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ watchEffect(() => {
})
const { mutateAsync, isPending } = useMutation({
mutationFn: async (param: { file: File }) => {
const { artifactId } = await onUpload(param.file, 'generate_bundle')
mutationFn: async (param: { file: File, fileApk: string | undefined }) => {
const { artifactId } = await onUpload(param.file, param.fileApk)
const groupIds = selectedGroup.value?.map(e => e.id) ?? []
if (artifactId && groupIds && groupIds.length) {
await $fetch('/api/update-artifact-groups', {
Expand All @@ -89,12 +89,14 @@ const submit = async () => {
if (!realFile) {
return
}
const generateFileApk = realFile.name.endsWith('.aab')
mutateAsync({
file: realFile,
fileApk: generateFileApk ? 'generate_bundle' : undefined,
})
}
const onUpload = async (file: File, fileApk: string) => {
const onUpload = async (file: File, fileApk: string | undefined) => {
const data = await uploadArtifact(file, orgName.value, appName.value, releaseNotes.value, fileApk)
return {
artifactId: data!.artifactId,
Expand Down
37 changes: 21 additions & 16 deletions server/api/artifacts/upload-artifact-url.post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { findApiKey } from "./upload-artifact.post"

export default defineEventHandler(async (event) => {
const { fileKey, apkFileKey, appName, orgName, releaseNotes, packageMetadata, } = await readValidatedBody(event, z.object({
const { uploadId, fileKey, apkFileKey, appName, orgName, releaseNotes, packageMetadata, } = await readValidatedBody(event, z.object({
uploadId: z.string().max(128),
fileKey: z.string().max(128),
apkFileKey: z.string().max(128).nullish(),
appName: z.string().trim().min(1).max(128),
Expand Down Expand Up @@ -63,21 +64,25 @@ export default defineEventHandler(async (event) => {
const newReleaseId = (lastArtifact?.releaseId ?? 0) + 1
const now = new Date()
const artifactsId = generateId()
await db.insert(tables.artifacts).values({
id: artifactsId,
createdAt: now,
updatedAt: now,
fileObjectKey: fileKey,
fileObjectApkKey: apkFileKey,
versionCode2: packageData?.versionCode?.toString()!,
versionName2: packageData?.versionName!,
appsId: appId,
organizationId: orgId,
releaseNotes: releaseNotes,
releaseId: newReleaseId,
extension: packageData?.extension,
packageName: packageData?.packageName,
})
await db.batch([
db.delete(tables.keyValue)
.where(eq(tables.keyValue.key, uploadId)),
db.insert(tables.artifacts).values({
id: artifactsId,
createdAt: now,
updatedAt: now,
fileObjectKey: fileKey,
fileObjectApkKey: apkFileKey,
versionCode2: packageData?.versionCode?.toString()!,
versionName2: packageData?.versionName!,
appsId: appId,
organizationId: orgId,
releaseNotes: releaseNotes,
releaseId: newReleaseId,
extension: packageData?.extension,
packageName: packageData?.packageName,
}),
])

return {
artifactId: artifactsId,
Expand Down
19 changes: 19 additions & 0 deletions server/api/artifacts/upload-artifact.post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { count } from "drizzle-orm"
import type { LibSQLDatabase } from "drizzle-orm/libsql"
import { uuidv7 } from "uuidv7"

export async function findApiKey(
db: LibSQLDatabase<typeof tables>,
Expand Down Expand Up @@ -103,7 +104,25 @@ export default defineEventHandler(async (event) => {

const { fileKey, signedUrl } = await generateSignedUrlUpload(orgId, appId)
const apkUrl = hasFileApk ? await generateSignedUrlUpload(orgId, appId) : undefined
const uploadId = uuidv7()
const now = new Date()
await db.insert(tables.keyValue)
.values({
key: uploadId,
value: {
fileKey,
orgId: orgId,
appId: appId,
...(apkUrl ? {
apkFileKey: apkUrl?.fileKey
} : undefined),
},
group: 'upload_temp',
createdAt: now,
updatedAt: now,
})
return {
uploadId,
fileKey,
url: signedUrl,
apkUrl: apkUrl ? {
Expand Down
6 changes: 6 additions & 0 deletions server/db/drizzle/0050_thick_titania.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `key_value` (
`key` text PRIMARY KEY NOT NULL,
`value` text,
`createdAt` integer,
`updatedAt` integer
);
1 change: 1 addition & 0 deletions server/db/drizzle/0051_narrow_medusa.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `key_value` ADD `group` text DEFAULT 'default';
1 change: 1 addition & 0 deletions server/db/drizzle/0052_acoustic_goliath.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX `key_value_key_group_unique` ON `key_value` (`key`,`group`);
Loading

0 comments on commit c8e692d

Please sign in to comment.