-
Notifications
You must be signed in to change notification settings - Fork 392
feat(storage): Add getDownloadUrl
method to the Storage API
#2036
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
Changes from 1 commit
3cc7ea6
f617907
0188a88
44cbde7
a93c8d3
925433f
07d3fc2
55362d5
5ff2ec0
d2cd1c0
8581f91
784aec4
2ede36b
8226d52
bcf7118
d32c44d
be58f54
a13b3e2
712d95c
e39c051
b6efb5f
116df89
21e9f04
d718edf
89d1176
8bfb21d
3b7e2ff
5e89622
d64713b
730c6f0
4443bf5
54f130e
66f41b5
a883e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
Bucket, | ||
BucketOptions, | ||
File, | ||
FileOptions, | ||
Storage as StorageClient, | ||
} from "@google-cloud/storage"; | ||
interface FirebaseMetadata { | ||
name: string; | ||
bucket: string; | ||
generation: string; | ||
metageneration: string; | ||
contentType: string; | ||
timeCreated: string; | ||
updated: string; | ||
storageClass: string; | ||
size: string; | ||
md5Hash: string; | ||
contentEncoding: string; | ||
contentDisposition: string; | ||
crc32c: string; | ||
etag: string; | ||
downloadTokens: string; | ||
} | ||
export class FirebaseStorageFile extends File { | ||
// Gets metadata from firebase backend instead of GCS | ||
getFirebaseMetadata(): Promise<FirebaseMetadata> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be private? |
||
// We need to talk to the firebase storage endpoints instead of the google cloud bucket endpoint | ||
const endpoint = "https://firebasestorage.googleapis.com/v0"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can configure this for testing against our internal testing endpoints or emulator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expectation is that you call |
||
// Build any custom headers based on the defined interceptors on the parent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this comment still accurate? |
||
// storage object and this object | ||
const storageInterceptors = this.storage?.interceptors || []; | ||
const fileInterceptors = this.interceptors || []; | ||
const allInterceptors = storageInterceptors.concat(fileInterceptors); | ||
const uri = `${endpoint}/b/${this.bucket.name}/o/${encodeURIComponent( | ||
this.name | ||
)}`; | ||
console.log(uri); | ||
maneesht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const headers = allInterceptors.reduce((acc, curInterceptor) => { | ||
maneesht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const currentHeaders = curInterceptor.request({ | ||
uri, | ||
}); | ||
|
||
Object.assign(acc, currentHeaders.headers); | ||
return acc; | ||
}, {}); | ||
return new Promise((resolve, reject) => { | ||
this.storage.makeAuthenticatedRequest( | ||
{ | ||
method: "GET", | ||
uri, | ||
headers, | ||
}, | ||
(err, body) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(body); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
|
||
async getDownloadUrl(): Promise<string> { | ||
const { downloadTokens } = await this.getFirebaseMetadata(); | ||
const [token] = downloadTokens.split(","); | ||
const baseUrl = | ||
process.env.STORAGE_EMULATOR_HOST || | ||
"https://firebasestorage.googleapis.com"; | ||
return `${baseUrl}/v0/b/${this.bucket.name}/o/${encodeURIComponent( | ||
this.name | ||
)}?alt=media&token=${token}`; | ||
} | ||
} | ||
|
||
export class FirebaseStorageBucket extends Bucket { | ||
file(name: string, options?: FileOptions): FirebaseStorageFile { | ||
return new FirebaseStorageFile(this, name, options); | ||
} | ||
} | ||
|
||
export class FirebaseStorageClient extends StorageClient { | ||
bucket(bucketName: string, options?: BucketOptions): FirebaseStorageBucket { | ||
return new FirebaseStorageBucket(this, bucketName, options); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.