91 lines
3.6 KiB
JavaScript
91 lines
3.6 KiB
JavaScript
var __create = Object.create;
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __getProtoOf = Object.getPrototypeOf;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
// file that has been converted to a CommonJS file using a Babel-
|
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
mod
|
|
));
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/upload/index.ts
|
|
var upload_exports = {};
|
|
__export(upload_exports, {
|
|
commonUpload: () => commonUpload,
|
|
upload: () => upload
|
|
});
|
|
module.exports = __toCommonJS(upload_exports);
|
|
var import_dayjs = __toESM(require("dayjs"));
|
|
var import_utils = require("../utils");
|
|
var import_base_64 = __toESM(require("base-64"));
|
|
var import_uuid = require("uuid");
|
|
var import_lodash_es = require("lodash-es");
|
|
var import_request = __toESM(require("@zhst/request"));
|
|
var defaultBucket = "public";
|
|
var commonUpload = async (file, option = {}, type) => {
|
|
const { bucket = defaultBucket, dir = "file", withSuFuffix = false } = option;
|
|
const prefix = `${(0, import_dayjs.default)().format("YYYYMMDD")}`;
|
|
const fileSuffix = withSuFuffix ? (0, import_utils.getFileSuffix)((0, import_lodash_es.get)(file, "name")) : "";
|
|
const fileType = file["type"].split("/", 2);
|
|
let imageSuffix = "";
|
|
if (fileType["0"] === "image") {
|
|
imageSuffix = (0, import_utils.getImageSuffixByFileType)(fileType["1"]);
|
|
}
|
|
const key = `${prefix}/${dir ? `${dir}/` : ""}${(0, import_uuid.v4)()}${!fileSuffix ? "" : `.${fileSuffix}`}`;
|
|
let imgKey = `${bucket}_${bucket}_${key}${imageSuffix}`;
|
|
if (type === "upload") {
|
|
imgKey = `v1_${import_base_64.default.encode(imgKey)}`;
|
|
return new Promise((resolve) => {
|
|
const reader = new FileReader();
|
|
reader.readAsDataURL(file);
|
|
reader.onload = async () => {
|
|
var _a;
|
|
if (reader.result) {
|
|
await (0, import_request.default)({
|
|
method: "PUT",
|
|
url: "/singer.FileServerService/PutObject",
|
|
data: {
|
|
version: 1,
|
|
bucket: defaultBucket,
|
|
objectName: `${bucket}_${key}${imageSuffix}`,
|
|
// fileData: reader.result,
|
|
putObjectOption: {
|
|
contentType: file.type
|
|
},
|
|
// @ts-ignore
|
|
fileDataBase64: (_a = reader.result) == null ? void 0 : _a.split(";base64,")[1]
|
|
}
|
|
});
|
|
resolve(imgKey);
|
|
}
|
|
};
|
|
});
|
|
}
|
|
};
|
|
var upload = async (file, option = {}) => {
|
|
return await commonUpload(file, option, "upload");
|
|
};
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
commonUpload,
|
|
upload
|
|
});
|