index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * Copyright 2020 Google Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.getDownloadURL = exports.getStorage = exports.Storage = void 0;
  20. const app_1 = require("../app");
  21. const storage_1 = require("./storage");
  22. const error_1 = require("../utils/error");
  23. const utils_1 = require("./utils");
  24. var storage_2 = require("./storage");
  25. Object.defineProperty(exports, "Storage", { enumerable: true, get: function () { return storage_2.Storage; } });
  26. /**
  27. * Gets the {@link Storage} service for the default app or a given app.
  28. *
  29. * `getStorage()` can be called with no arguments to access the default
  30. * app's `Storage` service or as `getStorage(app)` to access the
  31. * `Storage` service associated with a specific app.
  32. *
  33. * @example
  34. * ```javascript
  35. * // Get the Storage service for the default app
  36. * const defaultStorage = getStorage();
  37. * ```
  38. *
  39. * @example
  40. * ```javascript
  41. * // Get the Storage service for a given app
  42. * const otherStorage = getStorage(otherApp);
  43. * ```
  44. */
  45. function getStorage(app) {
  46. if (typeof app === 'undefined') {
  47. app = (0, app_1.getApp)();
  48. }
  49. const firebaseApp = app;
  50. return firebaseApp.getOrInitService('storage', (app) => new storage_1.Storage(app));
  51. }
  52. exports.getStorage = getStorage;
  53. /**
  54. * Gets the download URL for the given {@link https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/file | File}.
  55. *
  56. * @example
  57. * ```javascript
  58. * // Get the downloadUrl for a given file ref
  59. * const storage = getStorage();
  60. * const myRef = ref(storage, 'images/mountains.jpg');
  61. * const downloadUrl = await getDownloadURL(myRef);
  62. * ```
  63. */
  64. async function getDownloadURL(file) {
  65. const endpoint = (process.env.STORAGE_EMULATOR_HOST ||
  66. 'https://firebasestorage.googleapis.com') + '/v0';
  67. const { downloadTokens } = await (0, utils_1.getFirebaseMetadata)(endpoint, file);
  68. if (!downloadTokens) {
  69. throw new error_1.FirebaseError({
  70. code: 'storage/no-download-token',
  71. message: 'No download token available. Please create one in the Firebase Console.',
  72. });
  73. }
  74. const [token] = downloadTokens.split(',');
  75. return `${endpoint}/b/${file.bucket.name}/o/${encodeURIComponent(file.name)}?alt=media&token=${token}`;
  76. }
  77. exports.getDownloadURL = getDownloadURL;