obsService.ts 1.58 KB
import request from "@/utils/request";


export const parseAndDecodeUrl = (url:string) => {
  // 创建一个 URL 对象来解析传入的 URL 字符串
  const parsedUrl = new URL(url);
  
  // 获取 pathname (路径) 和 search (查询字符串, 如果有的话)
  // hash (锚点, 如果有的话) 也可以根据需要添加
  let { pathname, search = '', hash = '' } = parsedUrl;

  // 去掉路径最前面的斜杠
  pathname = pathname.startsWith('/') ? pathname.slice(1) : pathname;

  // 将路径、查询字符串和锚点部分进行解码
  pathname = decodeURIComponent(pathname);
  search = decodeURIComponent(search);
  hash = decodeURIComponent(hash);

  // 提取最后一个斜杠后面的内容(文件名或资源标识符)
  const lastSlashIndex = pathname.lastIndexOf('/');
  const lastPart = lastSlashIndex !== -1 ? pathname.substring(lastSlashIndex + 1) : pathname;

  // 返回去掉主机信息和开头斜杠后的解码部分以及最后一个斜杠后面的内容
  return {
      decodedPath: pathname + search + hash,
      lastPart: lastPart
  };
}

//获取下载签名
export const getDownFileSignByUrl = (fileName) => {
  return request({
    url: `${
      import.meta.env.VITE_APP_COMMON_URL
    }/obs/generate-download-file-signature?fileName=${fileName}`,
    method: "get",
  });
};

export const getImageContent = (params) => request({
  url: `${import.meta.env.VITE_APP_COMMON_URL}/obs/download-file-stream?filePath=${params.signedUrl}`,
  method: 'get',
  responseType: 'blob',
  headers: { 'Content-Type': params.actualSignedRequestHeaders['Content-Type'] }
});