1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const manager_node_1 = __importDefault(require("@cloudbase/manager-node"));
|
16 | const utils_1 = require("./utils");
|
17 | const error_1 = require("./error");
|
18 | function getStorageService(envId) {
|
19 | return __awaiter(this, void 0, void 0, function* () {
|
20 | const { secretId, secretKey, token } = yield utils_1.checkAndGetCredential(true);
|
21 | const app = new manager_node_1.default({
|
22 | secretId,
|
23 | secretKey,
|
24 | token,
|
25 | envId,
|
26 | proxy: utils_1.getProxy()
|
27 | });
|
28 | return app.storage;
|
29 | });
|
30 | }
|
31 | function uploadFile(options) {
|
32 | return __awaiter(this, void 0, void 0, function* () {
|
33 | const { envId, localPath, cloudPath } = options;
|
34 | const storageService = yield getStorageService(envId);
|
35 | return storageService.uploadFile({
|
36 | localPath,
|
37 | cloudPath
|
38 | });
|
39 | });
|
40 | }
|
41 | exports.uploadFile = uploadFile;
|
42 | function uploadDirectory(options) {
|
43 | return __awaiter(this, void 0, void 0, function* () {
|
44 | const { envId, localPath, cloudPath } = options;
|
45 | const storageService = yield getStorageService(envId);
|
46 | return storageService.uploadDirectory({
|
47 | localPath,
|
48 | cloudPath
|
49 | });
|
50 | });
|
51 | }
|
52 | exports.uploadDirectory = uploadDirectory;
|
53 | function downloadFile(options) {
|
54 | return __awaiter(this, void 0, void 0, function* () {
|
55 | const { envId, localPath, cloudPath } = options;
|
56 | const storageService = yield getStorageService(envId);
|
57 | return storageService.downloadFile({
|
58 | cloudPath,
|
59 | localPath
|
60 | });
|
61 | });
|
62 | }
|
63 | exports.downloadFile = downloadFile;
|
64 | function downloadDirectory(options) {
|
65 | return __awaiter(this, void 0, void 0, function* () {
|
66 | const { envId, localPath, cloudPath } = options;
|
67 | const storageService = yield getStorageService(envId);
|
68 | return storageService.downloadDirectory({
|
69 | cloudPath,
|
70 | localPath
|
71 | });
|
72 | });
|
73 | }
|
74 | exports.downloadDirectory = downloadDirectory;
|
75 | function deleteFile(options) {
|
76 | return __awaiter(this, void 0, void 0, function* () {
|
77 | const { envId, cloudPath, cloudPaths } = options;
|
78 | const storageService = yield getStorageService(envId);
|
79 | if (cloudPaths === null || cloudPaths === void 0 ? void 0 : cloudPaths.length) {
|
80 | return storageService.deleteFile(cloudPaths);
|
81 | }
|
82 | return storageService.deleteFile([cloudPath]);
|
83 | });
|
84 | }
|
85 | exports.deleteFile = deleteFile;
|
86 | function deleteDirectory(options) {
|
87 | return __awaiter(this, void 0, void 0, function* () {
|
88 | const { envId, cloudPath } = options;
|
89 | const storageService = yield getStorageService(envId);
|
90 | return storageService.deleteDirectory(cloudPath);
|
91 | });
|
92 | }
|
93 | exports.deleteDirectory = deleteDirectory;
|
94 | function list(options) {
|
95 | return __awaiter(this, void 0, void 0, function* () {
|
96 | const { envId, cloudPath } = options;
|
97 | const storageService = yield getStorageService(envId);
|
98 | return storageService.listDirectoryFiles(cloudPath);
|
99 | });
|
100 | }
|
101 | exports.list = list;
|
102 | function getUrl(options) {
|
103 | return __awaiter(this, void 0, void 0, function* () {
|
104 | const { envId, cloudPaths } = options;
|
105 | const storageService = yield getStorageService(envId);
|
106 | return storageService.getTemporaryUrl(cloudPaths);
|
107 | });
|
108 | }
|
109 | exports.getUrl = getUrl;
|
110 | function detail(options) {
|
111 | return __awaiter(this, void 0, void 0, function* () {
|
112 | const { envId, cloudPath } = options;
|
113 | const storageService = yield getStorageService(envId);
|
114 | return storageService.getFileInfo(cloudPath);
|
115 | });
|
116 | }
|
117 | exports.detail = detail;
|
118 | function getAcl(options) {
|
119 | return __awaiter(this, void 0, void 0, function* () {
|
120 | const { envId } = options;
|
121 | const storageService = yield getStorageService(envId);
|
122 | return storageService.getStorageAcl();
|
123 | });
|
124 | }
|
125 | exports.getAcl = getAcl;
|
126 | function setAcl(options) {
|
127 | return __awaiter(this, void 0, void 0, function* () {
|
128 | const { envId, acl } = options;
|
129 | const validAcl = ['READONLY', 'PRIVATE', 'ADMINWRITE', 'ADMINONLY'];
|
130 | if (!validAcl.includes(acl)) {
|
131 | throw new error_1.CloudBaseError('非法的权限值,仅支持:READONLY, PRIVATE, ADMINWRITE, ADMINONLY');
|
132 | }
|
133 | const storageService = yield getStorageService(envId);
|
134 | return storageService.setStorageAcl(acl);
|
135 | });
|
136 | }
|
137 | exports.setAcl = setAcl;
|