UNPKG

2 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2020 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18const fs = require("fs-extra");
19const os = require("os");
20const path = require("path");
21const util_1 = require("./util");
22function createStorageProvider() {
23 return new GoogleCloudStorageProvider(bucketName());
24}
25exports.createStorageProvider = createStorageProvider;
26class GoogleCloudStorageProvider {
27 constructor(bucket) {
28 this.bucket = bucket;
29 }
30 async retrieve(key, filePath) {
31 const targetFilePath = filePath || path.join(os.tmpdir() || "/tmp", util_1.guid());
32 await fs.ensureDir(path.dirname(targetFilePath));
33 const storage = new (await Promise.resolve().then(() => require("@google-cloud/storage"))).Storage();
34 await storage.bucket(this.bucket).file(key).download({ destination: targetFilePath });
35 return targetFilePath;
36 }
37 async store(key, filePath) {
38 const storage = new (await Promise.resolve().then(() => require("@google-cloud/storage"))).Storage();
39 await storage.bucket(this.bucket).upload(filePath, {
40 destination: key,
41 resumable: false,
42 });
43 }
44}
45exports.GoogleCloudStorageProvider = GoogleCloudStorageProvider;
46function bucketName() {
47 const bucket = process.env.ATOMIST_STORAGE || process.env.STORAGE;
48 return bucket.replace(/gs:\/\//, "");
49}
50//# sourceMappingURL=storage.js.map
\No newline at end of file