From 3c5e5aeb7c1231b8e8fda4a7a250e0b7525b69ec Mon Sep 17 00:00:00 2001 From: beajer <919060679@qq.com> Date: Tue, 21 Apr 2020 10:59:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=85=81=E8=AE=B8=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E8=AE=BE=E7=BD=AEhttp=E5=A4=B4,getMeta=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E9=80=9A=E8=BF=87ali-oss=20node=20api=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/services/oss2.js | 136 +++++++++++++++++- app/main/files/_/file-toolbar.html | 2 +- app/main/files/files.js | 11 +- .../files/modals/update-http-headers-modal.js | 10 +- 4 files changed, 146 insertions(+), 13 deletions(-) diff --git a/app/components/services/oss2.js b/app/components/services/oss2.js index 278562c9..697944bf 100644 --- a/app/components/services/oss2.js +++ b/app/components/services/oss2.js @@ -6,6 +6,7 @@ angular.module('web') var DEF_ADDR = 'oss://'; //var ALY = require('aliyun-sdk'); var path = require('path'); + var AliOSS = require('ali-oss'); return { createFolder: createFolder, @@ -13,9 +14,10 @@ angular.module('web') restoreFile: restoreFile, loadStorageStatus: loadStorageStatus, - getMeta: getMeta, - getFileInfo: getMeta, //head object + getMeta: getMeta2, + getFileInfo: getMeta2, //head object setMeta: setMeta, + setMeta2: setMeta2, checkFileExists: checkFileExists, checkFolderExists: checkFolderExists, @@ -75,6 +77,25 @@ angular.module('web') return client; } + function getClient3(opt){ + const options = prepaireOptions(opt); + const final = { + accessKeyId: options.accessKeyId, + accessKeySecret: options.secretAccessKey, + bucket: opt.bucket, + endpoint: options.endpoint, + region: opt.region, + timeout: options.httpOptions.timeout, + cname: options.cname, + isRequestPay: options.isRequestPayer + } + if(options.hasOwnProperty('securityToken')) { + final.stsToken = options.securityToken + } + const client = new AliOSS(final) + return client + } + function signatureUrl2(region, bucket, key, expires, xprocess) { var client = getClient2({ region: region, @@ -1031,6 +1052,109 @@ angular.module('web') }); } + function getMeta2(region, bucket, key) { + const client = getClient3({ region, bucket }); + function adapter(obj) { + const outputStructure = { + AcceptRanges: { + name: "accept-ranges", + }, + CacheControl: { + name: "Cache-Control", + }, + ContentDisposition: { + name: "Content-Disposition", + }, + ContentEncoding: { + name: "Content-Encoding", + }, + ContentLanguage: { + name: "Content-Language", + }, + ContentLength: { + type: "integer", + name: "Content-Length", + }, + ContentType: { + name: "Content-Type", + }, + DeleteMarker: { + type: "boolean", + name: "x-oss-delete-marker", + }, + ETag: { + name: "ETag", + }, + Expiration: { + name: "x-oss-expiration", + }, + Expires: { + type: "timestamp", + name: "Expires", + }, + LastModified: { + type: "timestamp", + name: "Last-Modified", + }, + // "Metadata": { + // "type": "map", + // "name": "x-oss-meta-", + // "members": {}, + // "keys": {} + // }, + MissingMeta: { + type: "integer", + name: "x-oss-missing-meta", + }, + Restore: { + name: "x-oss-restore", + }, + ServerSideEncryption: { + name: "x-oss-server-side-encryption", + }, + VersionId: { + name: "x-oss-version-id", + }, + WebsiteRedirectLocation: { + name: "x-oss-website-redirect-location", + }, + }; + const output = { + Metadata: obj.meta, + }; + const { hasOwnProperty } = Object.prototype; + const headers = obj.res.headers; + // extract output + for (let key in outputStructure) { + if (hasOwnProperty.call(outputStructure, key)) { + const name = outputStructure[key].name.toLowerCase(); + if (headers[name] !== undefined) { + output[key] = headers[name]; + } + } + } + // extract x-oss-... + for (let key in headers) { + if (key.indexOf("x-oss-") == 0) { + let arr = key.substring("x-oss-".length).split("-"); + for (let i = 0; i < arr.length; i++) { + arr[i] = arr[i][0].toUpperCase() + arr[i].substring(1); + } + output[arr.join("")] = headers[key]; + } else if (key === "content-md5") { + output["ContentMD5"] = headers["content-md5"]; + } + } + // extract requestId + output.RequestId = + headers["x-oss-request-id"] || headers["x-oss-requestid"]; + return output; + } + return client.head(key).then((res) => { + return adapter(res); + }); + } + function setMeta(region, bucket, key, headers, metas) { return new Promise(function (a, b) { var client = getClient({ @@ -1064,6 +1188,14 @@ angular.module('web') }); } + function setMeta2(region, bucket, key, headers, meta) { + const client = getClient2({region , bucket}); + return client.copy(key, key, { + headers, + meta + }).catch(handleError) + } + function restoreFile(region, bucket, key, days) { return new Promise(function (a, b) { diff --git a/app/main/files/_/file-toolbar.html b/app/main/files/_/file-toolbar.html index 8e085b3d..f8dc0584 100644 --- a/app/main/files/_/file-toolbar.html +++ b/app/main/files/_/file-toolbar.html @@ -120,7 +120,7 @@