Skip to content

Commit

Permalink
Add Undo Upload Image
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhuyule committed Jan 30, 2018
1 parent 4a99d29 commit 1b0a0d5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 55 deletions.
12 changes: 9 additions & 3 deletions app/moe-l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ class MoeditorLocale {

get(str) {
let res;
if (typeof strings[this.locale] === 'undefined' || typeof strings[this.locale][str] === 'undefined') {
res = strings['en'][str] || str;
console.log('Localization of "' + str + '" in "' + this.locale + '" failed, falling back to English.');
if (typeof strings[this.locale] == 'undefined' || typeof strings[this.locale][str] == 'undefined') {
res = strings['en'][str];
if (!res || res == undefined )
res = str;
// console.log('Localization of "' + str + '" in "' + this.locale + '" failed, falling back to English.');
} else {
res = strings[this.locale][str];
if (!res || res == undefined )
res = str;
}
return res;
}
Expand Down Expand Up @@ -234,6 +238,7 @@ const strings = {
"UploadToCOS": "Upload To COS",
"UploadToSMMS": "Upload To SM.MS",
"UploadToQiNiu": "Upload To QiNiu",
"UndoUpload": "Undo Uploaded",
"Quick Open":"Quick Open",
"OpenPathPost":"Open Post Path",
"OpenPathPostSrc":"Open Post Src Path",
Expand Down Expand Up @@ -408,6 +413,7 @@ const strings = {
"UploadToCOS": "上传 腾讯云",
"UploadToSMMS": "上传 SM.MS",
"UploadToQiNiu": "上传 七牛",
"UndoUpload": "撤销上传",
"Quick Open":"快速打开",
"OpenPathPost":"本文所在路径",
"OpenPathPostSrc":"本文图片路径",
Expand Down
91 changes: 47 additions & 44 deletions app/tool/hexo-cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var cosServer = (function () {
* @param bucket
* @param region
*/
update(acessKey, secretKey, bucket, region,protocol) {
update(acessKey, secretKey, bucket, region, protocol) {
this.cos.options.SecretId = acessKey || config.SecretId || '';
this.cos.options.SecretKey = secretKey || config.SecretKey || '';
config.SecretId = acessKey || config.SecretId || '';
Expand All @@ -55,14 +55,14 @@ var cosServer = (function () {

getService(cb) {
this.cos.getService(function (err, data) {
if(!err && !err && [200,204].includes(data.statusCode) ) {
if (!err && !err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -74,14 +74,14 @@ var cosServer = (function () {
Expires: 60,
Sign: true,
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
return url;
}
Expand All @@ -91,14 +91,14 @@ var cosServer = (function () {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -107,14 +107,14 @@ var cosServer = (function () {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -125,14 +125,14 @@ var cosServer = (function () {
Key: filename,
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -148,14 +148,14 @@ var cosServer = (function () {
}
}, function (err, data) {
if (typeof cb === 'function')
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -165,14 +165,14 @@ var cosServer = (function () {
Region: config.Region,
Key: fileanme
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
log.info(fileanme,' was deleted!')
if (!err && [200, 204].includes(data.statusCode)) {
log.info(`[${fileanme}]: was deleted!`)
data.statusCode = 200;
} else {
log.error(err.error.message);
} else {
log.error(`[${fileanme}]:` + err.error.message)
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand All @@ -185,14 +185,14 @@ var cosServer = (function () {
// {Key: '3mb.zip'},
// ]
}, function (err, data) {
if(!err && [200,204].includes(data.statusCode) ) {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(data)
data.statusCode = 200;
} else {
} else {
log.error(err.error.message);
}
if (typeof cb === 'function')
cb(err||data );
cb(err || data);
});
}

Expand Down Expand Up @@ -223,27 +223,27 @@ var cosServer = (function () {
}*/

if (typeof cb === "function") {
let result = {type: 100,id: localFile};
if (err){
let result = {type: 100, id: localFile};
if (err) {
result.type = 100;
result.statusCode = err.statusCode;
result.msg = err.error.message;
} else{
} else {
result.type = 200;
result.statusCode = data.statusCode;
result.data = {
localname: path.basename(localFile),
storename: path.basename(serverFile),
path: serverFile,
url: config.Protocol + data.Location.replace(/https?:\/\//,''),
url: config.Protocol + data.Location.replace(/https?:\/\//, ''),
}
}
cb(result)
} else {
if (err){
if (err) {
log.error(err.error.message);
} else{
log.info(config.Protocol + data.Location.replace(/https?:\/\//,''))
} else {
log.info(config.Protocol + data.Location.replace(/https?:\/\//, ''))
}
}
});
Expand Down Expand Up @@ -275,28 +275,30 @@ var cosServer = (function () {
* statusCode: 200,
* headers: {}
}*/
if(!err && [200,204].includes(data.statusCode) ) {
log.info(data)
data.statusCode = 200;
} else {
log.error(err.error.message);
}

if (typeof cb === "function") {
let result = {type: 100,id: localFile};
if (err){
let result = {type: 100, id: localFile};
if (err) {
result.statusCode = err.statusCode;
result.msg = err.error.message;
} else{
} else {
result.type = 200;
result.statusCode = data.statusCode;
result.data = {
localname: path.basename(localFile),
storename: path.basename(serverFile),
path: data.Key,
url: config.Protocol + data.Location,
url: config.Protocol + data.Location
}
}
cb(result)
} else {
if (!err && [200, 204].includes(data.statusCode)) {
log.info(config.Protocol + data.Location)
data.statusCode = 200;
} else {
log.error(err.error.message);
}
}
});
}
Expand All @@ -316,6 +318,7 @@ var cosServer = (function () {
console.log('restart');
}
}

return CosServer;
})();

Expand Down
5 changes: 3 additions & 2 deletions app/tool/hexo-smms.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ var smms = (function () {
var content = body;
if (content) {
content = content.match(/<div class="container"><div[^>]*>([^<]*)</)
if (content != null)
if (content != null){
log.info(`[${fileHash}]:`+content[1])
content = fileHash + ' : ' + content[1];
}
}
log.info(content)
})
}

Expand Down
8 changes: 4 additions & 4 deletions app/tool/hexo-uploadServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ module.exports = (function () {
let imgPath = result.id;
let nextType = '';
if( result.statusCode == 200){
log.info('sucess:' + result.id, result.data.path, result.data.url);
log.info(`sucess:[${result.id}]-->[${result.data.url}]`);
}else{
log.warn('failed:' + result.statusCode,result.id, result.msg);
log.warn(`failed:[${result.statusCode}][${+result.id}]-->[${result.msg}]`);
}

if (typeServer == 'smms' && typeBack > 2) { //是否需要备份
Expand Down Expand Up @@ -262,7 +262,7 @@ module.exports = (function () {
}
finishedCallback(info, successList, errorList);
isUploading = false;
log.info(`---End upload---[S: ${successList.length}| F: ${errorList.length}][time:${ new Date() - startDate}]`);
log.info(`---End upload---[S:${successList.length}| F:${errorList.length}][time:${ new Date() - startDate}]`);
}
}

Expand Down Expand Up @@ -332,7 +332,7 @@ module.exports = (function () {
return;
if (!(pathArray instanceof Array))
return;
log.info(`---begin upload---[${pathArray.length}]`);
log.info(`---Begin upload---[${pathArray.length}]`);
startDate = new Date();
isUploading = true;
baseDir = srcDir;
Expand Down
9 changes: 8 additions & 1 deletion views/main/hexo-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class ImgManager {
this.updateBase();
}

hasUploadFile(){
return (uploadList.length > 0)
}

updateBase() {
let rootPaht = '';
if (moeApp.config.get('image-source-center')) {
Expand Down Expand Up @@ -220,7 +224,7 @@ class ImgManager {
break;
}
}

uploadList = [];
}

uploadDelAll() {
Expand Down Expand Up @@ -276,6 +280,7 @@ class ImgManager {
let maxIndex = errorList.length - 1;
if (maxIndex >= 0) {
errorList.forEach((response, index) => {
log.warn(`Failed File:[${response.id}]==>[${__(response.msg)}]`);
errMsg += response.id + ':' + __(response.msg) + '</br>';
if (maxIndex == index) {
window.popMessageShell(null, {
Expand All @@ -298,6 +303,7 @@ class ImgManager {
function updateSrc(successList) {
let content = editor.getValue();
let maxIndex = successList.length - 1;
log.info(`Begin update Src[${maxIndex}]`);
successList.forEach((response, index) => {
uploadList.push(response);
content = content.replace(new RegExp(imgManager.imgPathToMarkURL[response.id], 'g'), response.data.url);
Expand All @@ -308,6 +314,7 @@ class ImgManager {
editor.setValue(content);
hexoWindow.content = content;
hexoWindow.changed = true;
log.info(`End update Src[${maxIndex}]`);
}
});
}
Expand Down
9 changes: 8 additions & 1 deletion views/main/moe-contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ document.addEventListener('DOMContentLoaded', () => {
label: imageMenuLable,
enabled: imageMenuEnable,
click(item, w) {
!imgManager.uploadLocalSrc();
imgManager.uploadLocalSrc();
}
},
{
label: __('UndoUpload'),
visible: imgManager.hasUploadFile(),
click(item, w) {
imgManager.abortUploading();
}
},
{
Expand Down

0 comments on commit 1b0a0d5

Please sign in to comment.