mirror of
https://gitee.com/dcloud/opendb.git
synced 2024-12-25 22:16:08 +08:00
feat: added uni-media-library
This commit is contained in:
parent
4db0f3c300
commit
9f8999547d
68
collection/uni-media-library/collection.json
Normal file
68
collection/uni-media-library/collection.json
Normal 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": []
|
||||
}
|
17
collection/uni-media-library/package.json
Normal file
17
collection/uni-media-library/package.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
72
collection/uni-media-library/schema.ext.js
Normal file
72
collection/uni-media-library/schema.ext.js
Normal 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("媒体资源应用缩略图规则失败,请看下云函数日志获取详细错误")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
65
collection/uni-media-library/schema.json
Normal file
65
collection/uni-media-library/schema.json
Normal 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": "媒体资源创建时间"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user