From 9f8999547d3a1fed6a89f7b7a45da09b7929c899 Mon Sep 17 00:00:00 2001 From: chenruilong Date: Mon, 29 May 2023 11:55:49 +0800 Subject: [PATCH] feat: added uni-media-library --- collection/uni-media-library/collection.json | 68 ++++++++++++++++++ collection/uni-media-library/package.json | 17 +++++ collection/uni-media-library/schema.ext.js | 72 ++++++++++++++++++++ collection/uni-media-library/schema.json | 65 ++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 collection/uni-media-library/collection.json create mode 100644 collection/uni-media-library/package.json create mode 100644 collection/uni-media-library/schema.ext.js create mode 100644 collection/uni-media-library/schema.json diff --git a/collection/uni-media-library/collection.json b/collection/uni-media-library/collection.json new file mode 100644 index 0000000..6f88014 --- /dev/null +++ b/collection/uni-media-library/collection.json @@ -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": [] +} diff --git a/collection/uni-media-library/package.json b/collection/uni-media-library/package.json new file mode 100644 index 0000000..f792e77 --- /dev/null +++ b/collection/uni-media-library/package.json @@ -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" + } + ] + } +} diff --git a/collection/uni-media-library/schema.ext.js b/collection/uni-media-library/schema.ext.js new file mode 100644 index 0000000..8905fe7 --- /dev/null +++ b/collection/uni-media-library/schema.ext.js @@ -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("媒体资源应用缩略图规则失败,请看下云函数日志获取详细错误") + } + } + } +} diff --git a/collection/uni-media-library/schema.json b/collection/uni-media-library/schema.json new file mode 100644 index 0000000..f90986c --- /dev/null +++ b/collection/uni-media-library/schema.json @@ -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": "媒体资源创建时间" + } + } +}