Skip to content

Commit

Permalink
feat: download project middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
4e6 committed Jan 30, 2025
1 parent a0e6311 commit 5b95795
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/gui/project-manager-shim-middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from 'node:fs/promises'
import * as http from 'node:http'
import * as https from 'node:https'
import * as path from 'node:path'
import * as stream from 'node:stream'

import * as tar from 'tar'
import * as yaml from 'yaml'
Expand Down Expand Up @@ -139,6 +140,36 @@ export default function projectManagerShimMiddleware(
)
} else if (requestUrl != null && requestUrl.startsWith('/api/cloud/')) {
switch (requestPath) {
case '/api/cloud/download-project': {
const url = new URL(`https://example.com/${requestUrl}`)
const downloadUrl = url.searchParams.get('download_url')

if (downloadUrl == null) {
response
.writeHead(HTTP_STATUS_BAD_REQUEST, COMMON_HEADERS)
.end('Request is missing search parameter `download_url`.')
break
}

const downloadRequest = https.request(downloadUrl, { method: 'GET' }, (actualResponse) => {
let data: Buffer[] = []

Check failure on line 155 in app/gui/project-manager-shim-middleware/index.ts

View workflow job for this annotation

GitHub Actions / 🧹 GUI Lint Results

app/gui/project-manager-shim-middleware/index.ts#L155

[prefer-const] 'data' is never reassigned. Use 'const' instead.

Check failure on line 155 in app/gui/project-manager-shim-middleware/index.ts

View workflow job for this annotation

GitHub Actions / 🧹 GUI Lint Results

app/gui/project-manager-shim-middleware/index.ts#L155

[prefer-const] 'data' is never reassigned. Use 'const' instead.

Check failure on line 155 in app/gui/project-manager-shim-middleware/index.ts

View workflow job for this annotation

GitHub Actions / 🧹 GUI Lint Results

app/gui/project-manager-shim-middleware/index.ts#L155

[prefer-const] 'data' is never reassigned. Use 'const' instead.

Check failure on line 155 in app/gui/project-manager-shim-middleware/index.ts

View workflow job for this annotation

GitHub Actions / 🧹 GUI Lint Results

app/gui/project-manager-shim-middleware/index.ts#L155

[prefer-const] 'data' is never reassigned. Use 'const' instead.
actualResponse
.on('data', (chunk) => data.push(Buffer.from(chunk, 'binary')))
.on('end', () => {
const buffer = Buffer.concat(data)
const readable = stream.Readable.from(buffer)
projectManagement.unpackBundle(readable).then((projectDirectory) => {
response
.writeHead(HTTP_STATUS_OK, COMMON_HEADERS)
.end({ path: projectDirectory })
})
})
})

downloadRequest.end()

break
}
case '/api/cloud/upload-project': {
const url = new URL(`https://example.com/${requestUrl}`)
const uploadUrl = url.searchParams.get('upload_url')
Expand Down
17 changes: 17 additions & 0 deletions app/gui/project-manager-shim-middleware/projectManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ export function createBundle(directory: string): stream.Readable {
)
}

/** Unpack a .tar.gz enso-project bundle into a temporary directory */
export async function unpackBundle(bundle: stream.Readable): Promise<string> {
const documentsDirectory = desktopEnvironment.DOCUMENTS || os.tmpdir()
const tempDirectory = fs.mkdtempSync(pathModule.join(documentsDirectory, 'cloud'))

return new Promise((resolve, reject) => {
bundle
.pipe(
tar.x({
C: tempDirectory,
}),
)
.on('finish', () => resolve(tempDirectory))
.on('error', (err) => reject(err))
})
}

// ================
// === Metadata ===
// ================
Expand Down

0 comments on commit 5b95795

Please sign in to comment.