Skip to content
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

[Feature]: 内置Server的上传接口支持上传 data URI (base64)形式文件 #251

Open
3 tasks done
0x0404 opened this issue Oct 6, 2024 · 1 comment
Open
3 tasks done
Assignees

Comments

@0x0404
Copy link

0x0404 commented Oct 6, 2024

前置阅读 | Pre-reading

PicList的版本 | PicList Version

v2.9.3

系统信息 | System Information

Mac

功能请求 | Feature request

内置Server的上传接口支持上传 data URI 形式的文件。

网页在展示某些图片时会使用内嵌的 data URI 形式的图片,简悦在上传这些图片到 PicList 时需要这个接口。

<img alt="" src="data:image/png;base64,iVBORw0KGgoAAA
ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU
5ErkJggg==" style="width:36pt;height:36pt" />

原始图片和 data URI 形式图片转换可以在这个网站进行测试。

上传接口可以参考 cloudinary 图床接口(不用一样,仅参考)。由于 curl 的 POST 上传会携带 x-www-form-urlencoded 请求头, data URI 数据还进行了 urlencoded 。

curl -X POST https://api.cloudinary.com/v1_1/<cloud name>/image/upload --data 'file=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4%2F%2F8%2Fw38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg%3D%3D&timestamp=1563412399&public_id=curlPost&api_key=<your api key>&signature=<sha-1 signature>'

data URI 形式数据拆分可以参考这部分源码

  // overload upload method to make options object optional
  // b64 string can include MIME or not, options can include mime or not
  // options override string for MIME
  // options has optional keys: headers, params, mime
  this.upload = function(b64, options, cb){
    if (arguments.length < 2 || arguments.length > 3){
      throw "Argument error. Make sure the arguments provided are valid.";
    }
    // handle two arguments provided
    if (arguments.length == 2){
      cb = options;
      options = {};
    }
    var descriptor = {};
    var commaIndex = b64.indexOf(",");
    if (commaIndex == -1){
      descriptor = {bare: b64, mime: null};
    } else {
      semicolonIndex = b64.indexOf(";");
      descriptor = {bare: b64.substring(commaIndex + 1), mime: b64.substring(5, semicolonIndex)};
    }
    var mime = descriptor.mime;
    if (options.mime){
      mime = options.mime;
    }
    if (!mime){
      throw "No mime specified. You need to specify a mime string (e.g. 'image/png') either in the base64 input or the options argument.";
    }
    file = new Buffer(descriptor.bare, 'base64');
    var paramsString = "";
    for (key in options.params){
      paramsString = paramsString.concat("&" + key + "=" + options.params[key]);
    }
    if (paramsString){
      paramsString = "?".concat(paramsString.substring(1));
    }
    var requestHeaders = {};
    if (options.headers){
      requestHeaders = options.headers;
    }
    requestHeaders["Content-Type"] = mime;
    var requestUrl = "";
    if (!options.url){
      if (!this.getApiUrl()){
        throw "No API URL specified. Use setApiUrl to set a URL";
      }
      requestUrl = this.getApiUrl() + paramsString;
    } else {
      requestUrl = options.url + paramsString;
    }
    request.post({url: requestUrl, body: file, headers: requestHeaders}, cb);
  }
}
@Kuingsmile
Copy link
Owner

感谢建议 我看下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants