租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
942 B

3 months ago
  1. const urlutil = require('url');
  2. const { isIP } = require('../utils/isIP');
  3. const proto = exports;
  4. /**
  5. * Get Object url by name
  6. * @param {String} name - object name
  7. * @param {String} [baseUrl] - If provide `baseUrl`, will use `baseUrl` instead the default `endpoint and bucket`.
  8. * @return {String} object url include bucket
  9. */
  10. proto.generateObjectUrl = function generateObjectUrl(name, baseUrl) {
  11. if (isIP(this.options.endpoint.hostname)) {
  12. throw new Error('can not get the object URL when endpoint is IP');
  13. }
  14. if (!baseUrl) {
  15. baseUrl = this.options.endpoint.format();
  16. const copyUrl = urlutil.parse(baseUrl);
  17. const { bucket } = this.options;
  18. copyUrl.hostname = `${bucket}.${copyUrl.hostname}`;
  19. copyUrl.host = `${bucket}.${copyUrl.host}`;
  20. baseUrl = copyUrl.format();
  21. } else if (baseUrl[baseUrl.length - 1] !== '/') {
  22. baseUrl += '/';
  23. }
  24. return baseUrl + this._escape(this._objectName(name));
  25. };