hexo-qiniu.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * This file is part of HexoEditor.
  3. *
  4. * Copyright (c) 2018 zhuzhuyule
  5. */
  6. class qiniuServer {
  7. constructor(acessKey, secretKey) {
  8. this.XMLHttpRequest = require('./XMLHttpRequest').XMLHttpRequest;
  9. this.qiniu = require('qiniu');
  10. this.bucket = '';
  11. this.qiniu.conf.ACCESS_KEY = acessKey;
  12. this.qiniu.conf.SECRET_KEY = secretKey;
  13. this.mac = new this.qiniu.auth.digest.Mac(acessKey, secretKey);
  14. }
  15. /**
  16. * 更新信息
  17. * @param acessKey
  18. * @param secretKey
  19. * @param bucket
  20. * @param url
  21. */
  22. update(acessKey, secretKey, bucket, url) {
  23. acessKey = acessKey || moeApp.config.get('image-qiniu-accessKey');
  24. secretKey = secretKey || moeApp.config.get('image-qiniu-secretKey');
  25. this.qiniu.conf.ACCESS_KEY = acessKey;
  26. this.qiniu.conf.SECRET_KEY = secretKey;
  27. this.mac = new this.qiniu.auth.digest.Mac(acessKey, secretKey);
  28. this.bucket = bucket || this.bucket || '';
  29. this.url = url || this.url || '';
  30. }
  31. /**
  32. * 生成空间 文件名
  33. * @param bucket 空间名(必传)
  34. * @param key Key值
  35. * @returns {string}
  36. */
  37. getUptoken(bucket, key) {
  38. var options = {
  39. scope: bucket + ":" + key
  40. };
  41. var putPolicy = new this.qiniu.rs.PutPolicy(options);
  42. return putPolicy.uploadToken();
  43. }
  44. /**
  45. * 生成 AccessToken
  46. * @param url
  47. * @returns {string}
  48. */
  49. getAccessToken(url) {
  50. return this.qiniu.util.generateAccessToken(this.mac, url);
  51. }
  52. /**
  53. * 异步获取空间列表
  54. */
  55. getBuckets(callback) {
  56. const url_api_bukets = 'https://rs.qbox.me/buckets';
  57. let xhr = new this.XMLHttpRequest();
  58. xhr.open('get', url_api_bukets);
  59. xhr.setRequestHeader('Authorization', this.getAccessToken(url_api_bukets));
  60. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  61. xhr.onreadystatechange = () => {
  62. if (xhr.readyState === 4) { // 成功完成
  63. if (typeof callback === "function") {
  64. callback({
  65. statusCode: xhr.status,
  66. data: JSON.parse(xhr.responseText)
  67. })
  68. }
  69. }
  70. }
  71. xhr.send();
  72. }
  73. /**
  74. * 异步获取空间地址URL列表
  75. * @param buketName 空间名(必传)
  76. */
  77. getBucketsUrl(buketName, callback) {
  78. const url_api_bukets = 'https://api.qiniu.com/v6/domain/list?tbl=' + buketName;
  79. let xhr = new this.XMLHttpRequest();
  80. xhr.open('get', url_api_bukets);
  81. xhr.setRequestHeader('Authorization', this.getAccessToken(url_api_bukets));
  82. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  83. xhr.onreadystatechange = () => {
  84. if (xhr.readyState === 4) { // 成功完成
  85. if (typeof callback === "function") {
  86. callback({
  87. statusCode: xhr.status,
  88. data: JSON.parse(xhr.responseText)
  89. })
  90. }
  91. }
  92. }
  93. xhr.send();
  94. }
  95. /**
  96. * 异步获取服务器文件列表
  97. * @param buketName 空间名称(必传)
  98. * @param prefix 虚拟目录(选填)
  99. */
  100. getBucketsFiles(buketName, prefix, callback) {
  101. if (!buketName) return;
  102. const url_api_bukets = require('util').format(
  103. 'https://rsf.qbox.me/list?bucket=%s&marker=&limit=1&prefix=%s&delimiter=/', buketName, prefix || '')
  104. let xhr = new this.XMLHttpRequest();
  105. xhr.open('get', url_api_bukets);
  106. xhr.setRequestHeader('Authorization', this.getAccessToken(url_api_bukets));
  107. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  108. xhr.onreadystatechange = () => {
  109. if (xhr.readyState === 4) { // 成功完成
  110. if (typeof callback === "function") {
  111. callback({
  112. statusCode: xhr.status,
  113. data: JSON.parse(xhr.responseText)
  114. })
  115. }
  116. }
  117. }
  118. xhr.send();
  119. }
  120. /**
  121. * 异步上传单个文件
  122. * @param localFile 本地文件全路径
  123. * @param serverFileName 服务器保存名称(可带地址)
  124. * @param callback callback(response) //回调函数
  125. * response = {
  126. * id: 'localFileAbsolutePath', //传入文件本地绝对路径
  127. * statusCode: 200|int, //服务器代码,200:正常,其他:报错
  128. * data: {
  129. * localname: 'abc.png', //本地文件名
  130. * storename: '5a6bea876702d.png', //服务器文件名,SM.MS随机生成
  131. * path: '/abc/abc/5a6bea876702d.png', //服务器路径
  132. * url: 'https://...../abc/abc/5a6bea876702d.png' //图片地址
  133. * },
  134. * msg: 'error message' //一般只有报错才使用到
  135. * errorlist: 'url' //一般只有报错才使用到
  136. * }
  137. */
  138. uploadFile(localFile, serverFileName, callback) {
  139. //生成上传 Token
  140. let token = this.getUptoken(this.bucket, serverFileName);
  141. var formUploader = new this.qiniu.form_up.FormUploader(this.qiniu.conf);
  142. var extra = new this.qiniu.form_up.PutExtra();
  143. let qiniuServer = this;
  144. formUploader.putFile(token, serverFileName, localFile, extra,
  145. function (respErr, respBody, respInfo) {
  146. if (typeof callback == 'function') {
  147. let result = {id: localFile};
  148. if (respInfo.statusCode == 200 || respInfo.statusCode == 579) {
  149. result.statusCode = 200;
  150. result.data = {
  151. localname: path.basename(localFile),
  152. storename: path.basename(serverFileName),
  153. path: respBody.key,
  154. url: qiniuServer.url + respBody.key
  155. }
  156. result.msg = '';
  157. result.errorlist = '';
  158. } else {
  159. result.msg = respInfo.statusCode + respBody.error;
  160. result.errorlist = 'https://developer.qiniu.com/kodo/api/3928/error-responses#2';
  161. }
  162. } else {
  163. console.log(respBody)
  164. }
  165. });
  166. }
  167. }
  168. module.exports = qiniuServer;