feat: added uni-media-library

This commit is contained in:
chenruilong 2023-05-29 11:55:49 +08:00
parent 4db0f3c300
commit 9f8999547d
4 changed files with 222 additions and 0 deletions

View File

@ -0,0 +1,68 @@
{
"schema": {
"bsonType": "object",
"required": [],
"permission": {
"read": true,
"create": true,
"update": true,
"delete": false
},
"properties": {
"_id": {
"description": "ID系统自动生成"
},
"src": {
"bsonType": "string",
"description": "媒体资源地址"
},
"cover": {
"bsonType": "string",
"description": "媒体资源封面地址;仅视频使用"
},
"type": {
"bsonType": "string",
"description": "媒体资源类型",
"enum": ["image", "video"]
},
"description": {
"bsonType": "string",
"description": "媒体资源描述"
},
"alt": {
"bsonType": "string",
"description": "媒体资源alt替代文字"
},
"originalName": {
"bsonType": "string",
"description": "媒体资源原始文件名"
},
"fileType": {
"bsonType": "string",
"description": "媒体资源文件类型"
},
"size": {
"bsonType": "number",
"description": "媒体资源大小"
},
"resolution": {
"bsonType": "object",
"description": "媒体资源分辨率"
},
"duration": {
"bsonType": "number",
"description": "媒体资源时长"
},
"uploadUser": {
"bsonType": "string",
"description": "媒体资源上传用户",
"foreignKey": "uni-id-users._id"
},
"createDate": {
"bsonType": "timestamp",
"description": "媒体资源创建时间"
}
}
},
"index": []
}

View File

@ -0,0 +1,17 @@
{
"name": "@opendb/uni-media-library",
"version": "0.0.1",
"description": "uni-media-library",
"keywords": ["uni-media-library","媒体库"],
"opendb": {
"title": "媒体库记录",
"type": "collection",
"contributors": [
{
"name": "DCloud",
"email": "",
"homepage": "https://gitee.com/dcloud/opendb"
}
]
}
}

View File

@ -0,0 +1,72 @@
const createConfig = safeRequire('uni-config-center')
const config = createConfig({
pluginId: 'uni-media-library'
}).config()
const coverCropRule = {
aliyun: {
listCover: '?x-oss-process=image/auto-orient,1/resize,m_fill,w_150,h_150/quality,q_80',
detailCover: '?x-oss-process=image/auto-orient,1/resize,m_fill,w_300,h_180/quality,q_80'
},
tencent: {
listCover: '?imageMogr2/thumbnail/!150x150r/quality/80',
detailCover: '?imageMogr2/thumbnail/!300x180r/quality/80',
}
}
function safeRequire(module) {
try {
return require(module)
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error(`${module} 公共模块不存在,请在 uniCloud/database 目录右击"配置schema扩展公共模块"添加 ${module} 模块`)
}
}
}
function getImageCropRule (url) {
return /^cloud:\/\//.test(url) ? coverCropRule.tencent : coverCropRule.aliyun
}
function getOssProcessUrls (url) {
const urls = {}
if (!url) return urls
const cropRules = getImageCropRule(url)
for (const key in cropRules) {
// webp 格式不支持缩略图
if (/.webp/.test(url) || !config.cropMediaAssets) {
urls[key] = url
} else {
urls[key] = url + cropRules[key]
}
}
return urls
}
function cropImage (result) {
if (result && result.data && result.data.length > 0) {
result.data.forEach(item => {
item.thumb = {}
if (item.type === 'video') {
item.thumb = getOssProcessUrls(item.cover)
} else {
item.thumb = getOssProcessUrls(item.src)
}
})
}
}
module.exports = {
trigger: {
afterRead: ({result}) => {
// 处理缩略图
try {
cropImage(result)
} catch (e) {
console.error(e)
throw new Error("媒体资源应用缩略图规则失败,请看下云函数日志获取详细错误")
}
}
}
}

View File

@ -0,0 +1,65 @@
{
"bsonType": "object",
"required": [],
"permission": {
"read": true,
"create": true,
"update": true,
"delete": true
},
"properties": {
"_id": {
"description": "ID系统自动生成"
},
"src": {
"bsonType": "string",
"description": "媒体资源地址"
},
"cover": {
"bsonType": "string",
"description": "媒体资源封面地址;仅视频使用"
},
"type": {
"bsonType": "string",
"description": "媒体资源类型",
"enum": ["image", "video"]
},
"description": {
"bsonType": "string",
"description": "媒体资源描述"
},
"alt": {
"bsonType": "string",
"description": "媒体资源alt替代文字"
},
"originalName": {
"bsonType": "string",
"description": "媒体资源原始文件名"
},
"fileType": {
"bsonType": "string",
"description": "媒体资源文件类型"
},
"size": {
"bsonType": "number",
"description": "媒体资源大小"
},
"resolution": {
"bsonType": "object",
"description": "媒体资源分辨率"
},
"duration": {
"bsonType": "number",
"description": "媒体资源时长"
},
"uploadUser": {
"bsonType": "string",
"description": "媒体资源上传用户",
"foreignKey": "uni-id-users._id"
},
"createDate": {
"bsonType": "timestamp",
"description": "媒体资源创建时间"
}
}
}