|
| 1 | +/*! |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {FirebaseApp} from '../firebase-app'; |
| 18 | +import {FirebaseError} from '../utils/error'; |
| 19 | +import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service'; |
| 20 | +import {ApplicationDefaultCredential} from '../auth/credential'; |
| 21 | +import {Bucket} from '@google-cloud/storage'; |
| 22 | + |
| 23 | +import * as validator from '../utils/validator'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Internals of a Storage instance. |
| 27 | + */ |
| 28 | +class StorageInternals implements FirebaseServiceInternalsInterface { |
| 29 | + /** |
| 30 | + * Deletes the service and its associated resources. |
| 31 | + * |
| 32 | + * @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted. |
| 33 | + */ |
| 34 | + public delete(): Promise<void> { |
| 35 | + // There are no resources to clean up. |
| 36 | + return Promise.resolve(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Storage service bound to the provided app. |
| 42 | + */ |
| 43 | +export class Storage implements FirebaseServiceInterface { |
| 44 | + public INTERNAL: StorageInternals = new StorageInternals(); |
| 45 | + |
| 46 | + private appInternal: FirebaseApp; |
| 47 | + private storageClient: any; |
| 48 | + |
| 49 | + /** |
| 50 | + * @param {Object} app The app for this Storage service. |
| 51 | + * @constructor |
| 52 | + */ |
| 53 | + constructor(app: FirebaseApp) { |
| 54 | + if (!validator.isNonNullObject(app) || !('options' in app)) { |
| 55 | + throw new FirebaseError({ |
| 56 | + code: 'storage/invalid-argument', |
| 57 | + message: 'First argument passed to admin.storage() must be a valid Firebase app instance.', |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + let storage; |
| 62 | + try { |
| 63 | + /* tslint:disable-next-line:no-var-requires */ |
| 64 | + storage = require('@google-cloud/storage'); |
| 65 | + } catch (e) { |
| 66 | + throw new FirebaseError({ |
| 67 | + code: 'storage/missing-dependencies', |
| 68 | + message: 'Failed to import the Cloud Storage client library for Node.js. ' |
| 69 | + + 'Make sure to install the "@google-cloud/storage" npm package.', |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + const cert = app.options.credential.getCertificate(); |
| 74 | + if (cert != null) { |
| 75 | + // cert is available when the SDK has been initialized with a service account JSON file, |
| 76 | + // or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable. |
| 77 | + this.storageClient = storage({ |
| 78 | + credentials: { |
| 79 | + private_key: cert.privateKey, |
| 80 | + client_email: cert.clientEmail, |
| 81 | + }, |
| 82 | + }); |
| 83 | + } else if (app.options.credential instanceof ApplicationDefaultCredential) { |
| 84 | + // Try to use the Google application default credentials. |
| 85 | + this.storageClient = storage(); |
| 86 | + } else { |
| 87 | + throw new FirebaseError({ |
| 88 | + code: 'storage/invalid-credential', |
| 89 | + message: 'Failed to initialize Google Cloud Storage client with the available credential. ' + |
| 90 | + 'Must initialize the SDK with a certificate credential or application default credentials ' + |
| 91 | + 'to use Cloud Storage API.', |
| 92 | + }); |
| 93 | + } |
| 94 | + this.appInternal = app; |
| 95 | + } |
| 96 | + |
| 97 | + public bucket(name?: string): Bucket { |
| 98 | + let bucketName; |
| 99 | + if (typeof name !== 'undefined') { |
| 100 | + bucketName = name; |
| 101 | + } else { |
| 102 | + bucketName = this.appInternal.options.storageBucket; |
| 103 | + } |
| 104 | + |
| 105 | + if (validator.isNonEmptyString(bucketName)) { |
| 106 | + return this.storageClient.bucket(bucketName); |
| 107 | + } |
| 108 | + throw new FirebaseError({ |
| 109 | + code: 'storage/invalid-argument', |
| 110 | + message: 'Bucket name not specified or invalid. Specify a valid bucket name via the ' + |
| 111 | + 'storageBucket option when initializing the app, or specify the bucket name ' + |
| 112 | + 'explicitly when calling the getBucket() method.', |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns the app associated with this Storage instance. |
| 118 | + * |
| 119 | + * @return {FirebaseApp} The app associated with this Storage instance. |
| 120 | + */ |
| 121 | + get app(): FirebaseApp { |
| 122 | + return this.appInternal; |
| 123 | + } |
| 124 | +}; |
0 commit comments