Skip to content

feat: 允许批量设置http头,getMeta改为通过ali-oss node api获取 #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 134 additions & 2 deletions app/components/services/oss2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ angular.module('web')
var DEF_ADDR = 'oss://';
//var ALY = require('aliyun-sdk');
var path = require('path');
var AliOSS = require('ali-oss');

return {
createFolder: createFolder,
createBucket: createBucket,
restoreFile: restoreFile,
loadStorageStatus: loadStorageStatus,

getMeta: getMeta,
getFileInfo: getMeta, //head object
getMeta: getMeta2,
getFileInfo: getMeta2, //head object
setMeta: setMeta,
setMeta2: setMeta2,

checkFileExists: checkFileExists,
checkFolderExists: checkFolderExists,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion app/main/files/_/file-toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</li>

<li>
<a href="" ng-if="sel.has.length==1 && !sel.has[0].isFolder" ng-click="showHttpHeaders(sel.has[0])">
<a href="" ng-if="sel | canSetHeader" ng-click="showHttpHeaders(sel.has)">
<i class="fa fa-cog"></i>
<!-- Http头 -->
{{'http.headers'|translate}}
Expand Down
11 changes: 7 additions & 4 deletions app/main/files/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
angular.module('web')
.filter('canSetHeader', function() {
return sel => sel && sel.has && sel.has.length && sel.has.every(f => !f.isFolder);
})
.controller('filesCtrl', ['$scope', '$rootScope', '$uibModal', '$timeout','$translate', 'AuthInfo', 'ossSvs2', 'settingsSvs', 'fileSvs', 'safeApply', 'Toast', 'Dialog',
function ($scope, $rootScope, $modal, $timeout, $translate, AuthInfo, ossSvs2, settingsSvs, fileSvs, safeApply, Toast, Dialog) {
var T = $translate.instant;
Expand Down Expand Up @@ -234,9 +237,9 @@ angular.module('web')
//Http头
return '<i class="fa fa-cog"></i> ' + T('http.headers')
}, function ($itemScope, $event) {
showHttpHeaders($scope.sel.has[0])
showHttpHeaders($scope.sel.has)
}, function(){
return $scope.sel.has && $scope.sel.has.length==1 && !$scope.sel.has[0].isFolder;
return $scope.sel.has && $scope.sel.has.length && $scope.sel.has.every(f => !f.isFolder);
}],

[function(){
Expand Down Expand Up @@ -1111,7 +1114,7 @@ angular.module('web')
controller: 'updateHttpHeadersModalCtrl',
resolve: {
item: function () {
return angular.copy(item);
return angular.copy(Array.isArray(item) ? item : [item]);
},
currentInfo: function () {
return angular.copy($scope.currentInfo);
Expand Down Expand Up @@ -1193,4 +1196,4 @@ angular.module('web')


}
]);
]);
10 changes: 4 additions & 6 deletions app/main/files/modals/update-http-headers-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ angular.module('web')
$scope.isLoading = true;
$scope.step=2;
var ignoreError = true;

ossSvs2.getMeta(currentInfo.region, currentInfo.bucket, item.path).then(function(result){

if(item.length > 1) return;
ossSvs2.getMeta(currentInfo.region, currentInfo.bucket, item[0].path).then(function(result){
$scope.headers = {
'ContentLanguage': result.ContentLanguage,
'ContentType': result.ContentType,
Expand Down Expand Up @@ -63,11 +62,10 @@ angular.module('web')
});
//console.log(headers, metas)
Toast.info(T('setting.on')); //'正在设置..'

ossSvs2.setMeta(currentInfo.region, currentInfo.bucket, item.path, headers, metas).then(function(result){
Promise.all(item.map(i => ossSvs2.setMeta2(currentInfo.region, currentInfo.bucket, i.path, headers, metas))).then(result => {
Toast.success(T('setting.success')); //'设置成功'
}).finally(() => {
cancel();

});
}

Expand Down