Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add download URL method to BlobsStorage #1469

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions packages/upload-api/src/blob/accept.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import { code as rawCode } from 'multiformats/codecs/raw'
import * as API from '../types.js'
import { AllocatedMemoryHadNotBeenWrittenTo } from './lib.js'

const R2_REGION = 'auto'
const R2_BUCKET = 'carpark-prod-0'

/**
* @param {API.W3ServiceContext} context
* @returns {API.ServiceMethod<API.BlobAccept, API.BlobAcceptSuccess, API.BlobAcceptFailure>}
Expand All @@ -32,21 +29,18 @@ export function blobAcceptProvider(context) {

const digest = Digest.decode(blob.digest)
const content = createLink(rawCode, digest)
const url =
/** @type {API.URI<'https:'>} */
(
`https://w3s.link/ipfs/${content}?format=raw&origin=${encodeURIComponent(
`r2://${R2_REGION}/${R2_BUCKET}`
)}`
)
const createUrl = await context.blobsStorage.createDownloadUrl(digest.bytes)
if (createUrl.error) {
return createUrl
}

const locationClaim = await Assert.location.delegate({
issuer: context.id,
audience: DID.parse(space),
with: context.id.toDIDKey(),
nb: {
content,
location: [url],
location: [createUrl.ok],
},
expiration: Infinity,
})
Expand Down
2 changes: 2 additions & 0 deletions packages/upload-api/src/types/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Result,
Failure,
DID,
URI,
} from '@ucanto/interface'
import {
Multihash,
Expand Down Expand Up @@ -84,4 +85,5 @@ export interface BlobsStorage {
Failure
>
>
createDownloadUrl: (content: Multihash) => Promise<Result<URI, Failure>>
}
9 changes: 4 additions & 5 deletions packages/upload-api/test/handlers/web3.storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { provisionProvider } from '../helpers/utils.js'
import { createServer, connect } from '../../src/lib.js'
import { alice, bob, createSpace, registerSpace } from '../util.js'
import { parseBlobAddReceiptNext } from '../helpers/blob.js'
import * as Result from '../helpers/result.js'

/**
* @type {API.Tests}
Expand Down Expand Up @@ -611,11 +612,9 @@ export const test = {
// @ts-expect-error nb unknown
const locations = delegation.capabilities[0].nb.location
assert.equal(locations.length, 1)
assert.ok(
locations[0].includes(
`https://w3s.link/ipfs/${content}?format=raw&origin=`
)
)

const loc = Result.unwrap(await context.blobsStorage.createDownloadUrl(digest))
assert.ok(locations.includes(loc))
},
'web3.storage/blob/accept fails to provide site delegation when blob was not stored':
async (assert, context) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/upload-api/test/storage/blobs-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,11 @@ export class BlobsStorage {
},
}
}

/** @param {Uint8Array} multihash */
async createDownloadUrl (multihash) {
const digest = digestDecode(multihash)
const url = new URL(this.#bucketPath(digest), this.baseURL)
return ok(/** @type {Types.URI} */ (url.toString()))
}
}
Loading