Skip to content

Commit

Permalink
Read package on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed May 1, 2024
1 parent d3e0db1 commit 7148e1e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
2 changes: 2 additions & 0 deletions components/AppFileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const submit = async () => {
const onUpload = async (file: File) => {
const packageMetadata = await readPackageFile(file)
const { url, file: key } = await $fetch('/api/artifacts/upload-artifact', {
method: 'post',
body: {
Expand All @@ -95,6 +96,7 @@ const onUpload = async (file: File) => {
key: key,
...prop.value,
releaseNotes: releaseNotes.value,
packageMetadata,
},
})
return {
Expand Down
23 changes: 8 additions & 15 deletions server/api/artifacts/upload-artifact-url.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CopyObjectCommand, DeleteObjectCommand, GetObjectCommand } from "@aws-s
import { S3AppClient } from "~/server/services/S3AppClient"

export default defineEventHandler(async (event) => {
const { key, appName, orgName, releaseNotes } = await readBody(event)
const { key, appName, orgName, releaseNotes, packageMetadata, } = await readBody(event)
const userId = event.context.auth.userId
const db = event.context.drizzle
const userOrg = await db.select({
Expand All @@ -22,25 +22,18 @@ export default defineEventHandler(async (event) => {
},
}).then(takeUniqueOrThrow)
const { temp, assets } = getStorageKeys(userOrg.organizationsId!, app.id, key)

// Processing the file
const s3 = new S3AppClient()
const tempSignedUrl = await s3.getSignedUrlGetObject(event, new GetObjectCommand({
Bucket: s3BucketName,
Key: temp,
}), 1800)
const response = await fetch(tempSignedUrl)
if (!response.ok) {
setResponseStatus(event, 400)
return
const packageData = packageMetadata as {
versionCode: string,
versionName: string,
packageName: string,
extension: string,
}
const arrayBuffer = await response.arrayBuffer() // WARNING: This will download to memory
const packageData = await readPackageFile(arrayBuffer)
if (!packageData) {
setResponseStatus(event, 400, 'Cannot read package')
return
}

const s3 = new S3AppClient()
await s3.copyObject(event, new CopyObjectCommand({
CopySource: `${s3BucketName}/${temp}`,
Bucket: s3BucketName,
Expand Down Expand Up @@ -74,7 +67,7 @@ export default defineEventHandler(async (event) => {
releaseNotes: releaseNotes,
releaseId: newReleaseId,
extension: packageData?.extension,
packageName: packageData?.metadata?.packageName,
packageName: packageData?.packageName,
})

return {
Expand Down
18 changes: 18 additions & 0 deletions server/api/manifest-parser-api.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ManifestParser from '~/utils/apkparser/manifest'

export default defineEventHandler(async (event) => {
if (!event.context.auth.userId) {
setResponseStatus(event, 401, 'Unauthorized')
return
}

const { base64 } = await readBody(event)
const androidManifestApk = Buffer.from(base64, 'base64')
const manifestParser = new ManifestParser(androidManifestApk, {}).parse()
const packageMetadata = {
versionCode: manifestParser.versionCode,
versionName: manifestParser.versionName,
packageName: manifestParser.package,
}
return packageMetadata
})
File renamed without changes.
File renamed without changes.
33 changes: 23 additions & 10 deletions server/utils/package-reader.ts → utils/package-reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse } from "@plist/parse"
import JSZip from "jszip"
import ManifestParser from '../apkparser/manifest'

export const readPackageFile = async (data: File | Buffer | ArrayBuffer | string | Blob) => {
var fileZip = new JSZip();
Expand All @@ -22,14 +21,28 @@ export const readPackageFile = async (data: File | Buffer | ArrayBuffer | string
}

// it's android apk
const androidManifestApk = await fileZip.file('AndroidManifest.xml')?.async('nodebuffer')
if (androidManifestApk) {
extension = 'apk'
const manifestParser = new ManifestParser(androidManifestApk, {}).parse()
packageMetadata = {
versionCode: manifestParser.versionCode,
versionName: manifestParser.versionName,
packageName: manifestParser.packageName,
const androidManifestApkEntry = fileZip.file('AndroidManifest.xml')
if (androidManifestApkEntry) {
if (process.server) {
const androidManifestApk = await androidManifestApkEntry?.async('nodebuffer')
if (androidManifestApk) {
extension = 'apk'
const ManifestParser = await import('~/utils/apkparser/manifest')
const manifestParser = new ManifestParser.default(androidManifestApk, {}).parse()
packageMetadata = {
versionCode: manifestParser.versionCode,
versionName: manifestParser.versionName,
packageName: manifestParser.package,
}
}
} else {
const base64Manifest = await androidManifestApkEntry.async('base64')
packageMetadata = await $fetch('/api/manifest-parser-api', {
method: 'post',
body: {
base64: base64Manifest,
}
})
}
}

Expand All @@ -54,7 +67,7 @@ export const readPackageFile = async (data: File | Buffer | ArrayBuffer | string
versionName: packageMetadata?.versionName!,
versionCode: parseInt(packageMetadata?.versionCode!),
extension: extension,
metadata: packageMetadata,
packageName: packageMetadata.packageName,
}
return packageDetail
}

0 comments on commit 7148e1e

Please sign in to comment.