-
Notifications
You must be signed in to change notification settings - Fork 373
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(storage): Add getDownloadUrl
method to the Storage API
#2036
Merged
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
3cc7ea6
Added getDownloadUrl to storage spec
maneesht f617907
Removed unnecessary var assignment
maneesht 0188a88
Updated api
maneesht 44cbde7
Created download tokens if not available
maneesht a93c8d3
Addressed comments
maneesht 925433f
Updated to fix endpoint
maneesht 07d3fc2
Removed file
maneesht 55362d5
Made storage modifiable
maneesht 5ff2ec0
Updated to stop passing endpoint info around
maneesht d2cd1c0
Removed only
maneesht 8581f91
Removed it only
maneesht 784aec4
Modified to use top-level instead of extending gcs API
maneesht 2ede36b
Addressed comments
maneesht 8226d52
Updated docs
maneesht bcf7118
Merge remote-tracking branch 'origin/master' into mtewani/add-downloa…
maneesht d32c44d
Updated lockfile
maneesht be58f54
Updated storage integration test
maneesht a13b3e2
Updated md
maneesht 712d95c
Merge branch 'master' into mtewani/add-download-link-storage
maneesht e39c051
Addresesd comments
maneesht b6efb5f
Reverted package-lock changes
maneesht 116df89
Reverted from origin
maneesht 21e9f04
Merge remote-tracking branch 'origin/master' into mtewani/add-downloa…
maneesht d718edf
Replaced in with of
maneesht 89d1176
Updated env setting
maneesht 8bfb21d
Fixed linting errors
maneesht 3b7e2ff
Fixed test
maneesht 5e89622
Undid fix
maneesht d64713b
Made sure tests worked
maneesht 730c6f0
Removed extra env var
maneesht 4443bf5
Addressed comments
maneesht 54f130e
Deleted file ref
maneesht 66f41b5
Fixed integration tests
maneesht a883e1b
Merge branch 'master' into mtewani/add-download-link-storage
maneesht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,31 @@ | |
* @packageDocumentation | ||
*/ | ||
|
||
import { App, getApp } from '../app'; | ||
import { FirebaseApp } from '../app/firebase-app'; | ||
import { Storage } from './storage'; | ||
import { File } from "@google-cloud/storage"; | ||
import { App, getApp } from "../app"; | ||
import { FirebaseApp } from "../app/firebase-app"; | ||
import { Storage } from "./storage"; | ||
import { FirebaseError } from "../utils/error"; | ||
|
||
export { Storage } from './storage'; | ||
export { Storage } from "./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; | ||
} | ||
|
||
/** | ||
* Gets the {@link Storage} service for the default app or a given app. | ||
|
@@ -46,10 +66,64 @@ export { Storage } from './storage'; | |
* ``` | ||
*/ | ||
export function getStorage(app?: App): Storage { | ||
if (typeof app === 'undefined') { | ||
if (typeof app === "undefined") { | ||
app = getApp(); | ||
} | ||
|
||
const firebaseApp: FirebaseApp = app as FirebaseApp; | ||
return firebaseApp.getOrInitService('storage', (app) => new Storage(app)); | ||
return firebaseApp.getOrInitService("storage", (app) => new Storage(app)); | ||
} | ||
/** | ||
* Gets metadata from Firebase backend instead of GCS | ||
* @returns {FirebaseMetadata} | ||
*/ | ||
function getFirebaseMetadata( | ||
endpoint: string, | ||
file: File | ||
): Promise<FirebaseMetadata> { | ||
// 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 up to date? |
||
// storage object and this object | ||
const uri = `${endpoint}/b/${file.bucket.name}/o/${encodeURIComponent( | ||
file.name | ||
)}`; | ||
|
||
return new Promise((resolve, reject) => { | ||
file.storage.makeAuthenticatedRequest( | ||
{ | ||
method: "GET", | ||
uri, | ||
}, | ||
(err, body) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(body); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the download URL for a given file. Will throw a `FirebaseError` if there are no download tokens available. | ||
* @returns {Promise<string>} | ||
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. same comment |
||
*/ | ||
|
||
export async function getDownloadUrl(file: File) { | ||
const endpoint = | ||
(process.env.STORAGE_EMULATOR_HOST || | ||
process.env.STORAGE_HOST_OVERRIDE || | ||
maneesht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"https://firebasestorage.googleapis.com") + "/v0"; | ||
const { downloadTokens } = await getFirebaseMetadata(endpoint, file); | ||
if (!downloadTokens) { | ||
throw new FirebaseError({ | ||
code: "storage/no-download-token", | ||
message: | ||
"No download token available. Please create one in the Firebase Console.", | ||
}); | ||
} | ||
const [token] = downloadTokens.split(","); | ||
return `${endpoint}/b/${file.bucket.name}/o/${encodeURIComponent( | ||
file.name | ||
)}?alt=media&token=${token}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small nit: jsdoc clauses unnecessary unless they add context that cant be inferred from the method signature
go/java-practices/javadoc#param