From 1ea4e011dc0c39cbe171970b5cba43994dcdf8cd Mon Sep 17 00:00:00 2001 From: Xziy Date: Wed, 11 Sep 2024 17:24:00 +0300 Subject: [PATCH] add item --- libs/adminpanel/ProductMediaManager/Item.d.ts | 31 --- libs/adminpanel/ProductMediaManager/Item.js | 145 ------------ libs/adminpanel/ProductMediaManager/Item.ts | 170 -------------- libs/adminpanel/ProductMediaManager/Items.ts | 219 ++++++++++++++++++ .../ProductMediaManager.ts | 65 +++--- 5 files changed, 250 insertions(+), 380 deletions(-) delete mode 100644 libs/adminpanel/ProductMediaManager/Item.d.ts delete mode 100644 libs/adminpanel/ProductMediaManager/Item.js delete mode 100644 libs/adminpanel/ProductMediaManager/Item.ts create mode 100644 libs/adminpanel/ProductMediaManager/Items.ts diff --git a/libs/adminpanel/ProductMediaManager/Item.d.ts b/libs/adminpanel/ProductMediaManager/Item.d.ts deleted file mode 100644 index 573f94bc..00000000 --- a/libs/adminpanel/ProductMediaManager/Item.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { File, Item, UploaderFile, imageSizes } from 'sails-adminpanel/lib/media-manager/AbstractMediaManager'; -import sharp from "sharp"; -export declare class ImageItem extends File { - type: "application" | "audio" | "example" | "image" | "message" | "model" | "multipart" | "text" | "video"; - constructor(path: string, dir: string, model: string, metaModel: string); - getItems(limit: number, skip: number, sort: string): Promise<{ - data: Item[]; - next: boolean; - }>; - search(s: string): Promise; - upload(file: UploaderFile, filename: string, origFileName: string, imageSizes?: imageSizes | {}): Promise; - getChildren(id: string): Promise; - protected createSizes(file: UploaderFile, parent: Item, filename: string, imageSizes: imageSizes): Promise; - protected createThumb(id: string, file: UploaderFile, filename: string, origFileName: string): Promise; - protected createEmptyMeta(id: string): Promise; - getMeta(id: string): Promise<{ - key: string; - value: string; - }[]>; - setMeta(id: string, data: { - [p: string]: string; - }): Promise<{ - msg: "success"; - }>; - protected resizeImage(input: string, output: string, width: number, height: number): Promise; - uploadCropped(parent: Item, file: UploaderFile, filename: string, config: { - width: number; - height: number; - }): Promise; - delete(id: string): Promise; -} diff --git a/libs/adminpanel/ProductMediaManager/Item.js b/libs/adminpanel/ProductMediaManager/Item.js deleted file mode 100644 index 5c006d67..00000000 --- a/libs/adminpanel/ProductMediaManager/Item.js +++ /dev/null @@ -1,145 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ImageItem = void 0; -const AbstractMediaManager_1 = require("sails-adminpanel/lib/media-manager/AbstractMediaManager"); -const MediaManagerHelper_1 = require("sails-adminpanel/lib/media-manager/helpers/MediaManagerHelper"); -const image_size_1 = __importDefault(require("image-size")); -const sharp_1 = __importDefault(require("sharp")); -class ImageItem extends AbstractMediaManager_1.File { - constructor(path, dir, model, metaModel) { - super(path, dir, model, metaModel); - this.type = "image"; - } - async getItems(limit, skip, sort) { - let data = await sails.models[this.model].find({ - where: { parent: null, mimeType: { contains: this.type } }, - limit: limit, - skip: skip, - sort: sort //@ts-ignore - }).populate('children', { sort: sort }); - let next = (await sails.models[this.model].find({ - where: { parent: null, mimeType: { contains: this.type } }, - limit: limit, - skip: skip === 0 ? limit : skip + limit, - sort: sort - })).length; - return { - data: data, - next: !!next - }; - } - async search(s) { - return await sails.models[this.model].find({ - where: { filename: { contains: s }, mimeType: { contains: this.type }, parent: null }, - sort: 'createdAt DESC' - }).populate('children', { sort: 'createdAt DESC' }); - } - async upload(file, filename, origFileName, imageSizes) { - let parent = await sails.models[this.model].create({ - parent: null, - mimeType: file.type, - size: file.size, - path: file.fd, - cropType: 'origin', - filename: origFileName, - image_size: (0, image_size_1.default)(file.fd), - url: `/${this.path}/${filename}` - }).fetch(); - await this.createEmptyMeta(parent.id); - if (parent.image_size.width > 150 && parent.image_size.height > 150) { - await this.createThumb(parent.id, file, filename, origFileName); - } - if (Object.keys(imageSizes).length) { - await this.createSizes(file, parent, filename, imageSizes); - } - return sails.models[this.model].find({ - where: { id: parent.id } - }).populate('children'); - } - async getChildren(id) { - return (await sails.models[this.model].findOne({ - where: { id: id } - }).populate('children', { sort: 'createdAt DESC' })).children; - } - async createSizes(file, parent, filename, imageSizes) { - for (const sizeKey of Object.keys(imageSizes)) { - let sizeName = (0, MediaManagerHelper_1.randomFileName)(filename, sizeKey, false); - let { width, height } = imageSizes[sizeKey]; - if (parent.image_size.width < width || parent.image_size.height < height) - continue; - let newFile = await this.resizeImage(file.fd, `${this.dir}${sizeName}`, width, height); - await sails.models[this.model].create({ - parent: parent.id, - mimeType: parent.mimeType, - size: newFile.size, - filename: parent.filename, - path: `${this.dir}${sizeName}`, - cropType: sizeKey, - image_size: (0, image_size_1.default)(`${this.dir}${sizeName}`), - url: `/${this.path}/${sizeName}` - }); - } - } - async createThumb(id, file, filename, origFileName) { - const thumbName = (0, MediaManagerHelper_1.randomFileName)(filename, 'thumb', false); - const thumb = await this.resizeImage(file.fd, `${this.dir}${thumbName}`, 150, 150); - await sails.models[this.model].create({ - parent: id, - mimeType: file.type, - size: thumb.size, - cropType: 'thumb', - path: `${this.dir}${thumbName}`, - filename: origFileName, - image_size: (0, image_size_1.default)(`${this.dir}${thumbName}`), - url: `/${this.path}/${thumbName}` - }); - } - async createEmptyMeta(id) { - //create empty meta - let metaData = { - author: "", - description: "", - title: "" - }; - for (const key of Object.keys(metaData)) { - await sails.models[this.metaModel].create({ - key: key, - value: metaData[key], - parent: id - }); - } - } - async getMeta(id) { - return (await sails.models[this.model].findOne(id).populate('meta')).meta; - } - async setMeta(id, data) { - for (const key of Object.keys(data)) { - await sails.models[this.metaModel].update({ parent: id, key: key }, { value: data[key] }); - } - return { msg: "success" }; - } - async resizeImage(input, output, width, height) { - return await (0, sharp_1.default)(input) - .resize({ width: width, height: height }) - .toFile(output); - } - async uploadCropped(parent, file, filename, config) { - return await sails.models[this.model].create({ - parent: parent.id, - mimeType: file.type, - size: file.size, - path: file.fd, - cropType: `${config.width}x${config.height}`, - filename: parent.filename, - image_size: (0, image_size_1.default)(file.fd), - url: `/${this.path}/${filename}` - }).fetch(); - } - async delete(id) { - await sails.models[this.model].destroy({ where: { id: id } }).fetch(); - } -} -exports.ImageItem = ImageItem; diff --git a/libs/adminpanel/ProductMediaManager/Item.ts b/libs/adminpanel/ProductMediaManager/Item.ts deleted file mode 100644 index ca075184..00000000 --- a/libs/adminpanel/ProductMediaManager/Item.ts +++ /dev/null @@ -1,170 +0,0 @@ -import {File, Item, UploaderFile, imageSizes} from 'sails-adminpanel/lib/media-manager/AbstractMediaManager' -import {randomFileName} from "sails-adminpanel/lib/media-manager/helpers/MediaManagerHelper"; -import sizeOf from "image-size"; -import sharp from "sharp"; - -interface Meta { - [key: string]: string -} - -export class ImageItem extends File { - public type: "application" | "audio" | "example" | "image" | "message" | "model" | "multipart" | "text" | "video" = "image"; - - constructor(path: string, dir: string, model: string, metaModel: string) { - super(path, dir, model, metaModel); - } - - public async getItems(limit: number, skip: number, sort: string): Promise<{ data: Item[]; next: boolean }> { - let data: Item[] = await sails.models[this.model].find({ - where: {parent: null, mimeType: { contains: this.type}}, - limit: limit, - skip: skip, - sort: sort//@ts-ignore - }).populate('children', {sort: sort}) - - let next: number = (await sails.models[this.model].find({ - where: {parent: null, mimeType: { contains: this.type}}, - limit: limit, - skip: skip === 0 ? limit : skip + limit, - sort: sort - })).length - - return { - data: data, - next: !!next - } - } - - public async search(s: string): Promise{ - return await sails.models[this.model].find({ - where: {filename: {contains: s}, mimeType: { contains: this.type}, parent: null}, - sort: 'createdAt DESC' - }).populate('children', {sort: 'createdAt DESC'}) - } - - public async upload(file: UploaderFile, filename: string, origFileName: string, imageSizes?: imageSizes | {}): Promise { - - let parent: Item = await sails.models[this.model].create({ - parent: null, - mimeType: file.type, - size: file.size, - path: file.fd, - cropType: 'origin', - filename: origFileName, - image_size: sizeOf(file.fd), - url: `/${this.path}/${filename}` - }).fetch(); - - await this.createEmptyMeta(parent.id) - - if (parent.image_size.width > 150 && parent.image_size.height > 150) { - await this.createThumb(parent.id, file, filename, origFileName) - } - - if (Object.keys(imageSizes).length) { - await this.createSizes(file, parent, filename, imageSizes) - } - - return sails.models[this.model].find({ - where: {id: parent.id} - }).populate('children') as Item; - } - - public async getChildren(id: string): Promise { - return (await sails.models[this.model].findOne({ - where: {id: id} - }).populate('children', {sort: 'createdAt DESC'})).children - } - - protected async createSizes(file: UploaderFile, parent: Item, filename: string, imageSizes: imageSizes): Promise { - for (const sizeKey of Object.keys(imageSizes)) { - let sizeName = randomFileName(filename, sizeKey, false) - - let {width, height} = imageSizes[sizeKey] - - if (parent.image_size.width < width || parent.image_size.height < height) continue - - let newFile = await this.resizeImage(file.fd, `${this.dir}${sizeName}`, width, height) - await sails.models[this.model].create({ - parent: parent.id, - mimeType: parent.mimeType, - size: newFile.size, - filename: parent.filename, - path: `${this.dir}${sizeName}`, - cropType: sizeKey, - image_size: sizeOf(`${this.dir}${sizeName}`), - url: `/${this.path}/${sizeName}` - }) - } - } - - protected async createThumb(id: string, file: UploaderFile, filename: string, origFileName: string): Promise { - const thumbName = randomFileName(filename, 'thumb', false) - const thumb = await this.resizeImage(file.fd, `${this.dir}${thumbName}`, 150, 150) - - await sails.models[this.model].create({ - parent: id, - mimeType: file.type, - size: thumb.size, - cropType: 'thumb', - path: `${this.dir}${thumbName}`, - filename: origFileName, - image_size: sizeOf(`${this.dir}${thumbName}`), - url: `/${this.path}/${thumbName}` - }) - } - - protected async createEmptyMeta(id: string) { - //create empty meta - let metaData: Meta = { - author: "", - description: "", - title: "" - } - - for (const key of Object.keys(metaData)) { - await sails.models[this.metaModel].create({ - key: key, - value: metaData[key], - parent: id - }) - } - } - - public async getMeta(id: string): Promise<{ key: string, value: string }[]> { - return (await sails.models[this.model].findOne(id).populate('meta')).meta - } - - async setMeta(id: string, data: { [p: string]: string }): Promise<{ msg: "success" }> { - for (const key of Object.keys(data)) { - await sails.models[this.metaModel].update({parent: id, key: key}, {value: data[key]}) - } - return {msg: "success"} - } - - protected async resizeImage(input: string, output: string, width: number, height: number) { - return await sharp(input) - .resize({width: width, height: height}) - .toFile(output) - } - - public async uploadCropped(parent: Item, file: UploaderFile, filename: string, config: { - width: number, - height: number - }): Promise { - return await sails.models[this.model].create({ - parent: parent.id, - mimeType: file.type, - size: file.size, - path: file.fd, - cropType: `${config.width}x${config.height}`, - filename: parent.filename, - image_size: sizeOf(file.fd), - url: `/${this.path}/${filename}` - }).fetch() - } - - async delete(id: string): Promise { - await sails.models[this.model].destroy({where: {id: id}}).fetch() - } -} \ No newline at end of file diff --git a/libs/adminpanel/ProductMediaManager/Items.ts b/libs/adminpanel/ProductMediaManager/Items.ts new file mode 100644 index 00000000..7238f750 --- /dev/null +++ b/libs/adminpanel/ProductMediaManager/Items.ts @@ -0,0 +1,219 @@ +import {File, Item, MediaFileType, UploaderFile, imageSizes} from 'sails-adminpanel/lib/media-manager/AbstractMediaManager' +import {randomFileName} from "sails-adminpanel/lib/media-manager/helpers/MediaManagerHelper"; +import sizeOf from "image-size"; +import sharp from "sharp"; +import { MediaFileRecord } from '../../../models/MediaFile'; + +interface Meta { + [key: string]: string +} + +export class ImageItem extends File { + public type: MediaFileType = "image"; + + constructor(path: string, dir: string, model: string, metaModel: string) { + super(path, dir, null, null); + } + + private MF2Item(mf: MediaFileRecord): Item + private MF2Item(mf: MediaFileRecord[]): Item[] + private MF2Item(mf: MediaFileRecord | MediaFileRecord[]): Item | Item[] { + let processItem = (mfItem: MediaFileRecord): Item => { + return { + id: mfItem.id, + parent: null, + children: [], + mimeType: 'image/*', + path: mfItem.original, + size: 0, + image_size: { + width: 0, + height: 0, + type: '' + }, + cropType: '', + url: mfItem.variant.origin, + filename: '', + meta: [] + }; + }; + + if (Array.isArray(mf)) { + return mf.map((mfItem) => processItem(mfItem)); + } else { + return processItem(mf); + } + } + + private Item2MF(item: Partial): MediaFileRecord + private Item2MF(items: Partial[]): MediaFileRecord[] + private Item2MF(itemOrItems: Partial | Partial[]): MediaFileRecord | MediaFileRecord[] { + let processItem = (item: Partial): MediaFileRecord => { + return { + id: item.id, + original: item.path, + variant: { + origin: item.url + } + }; + }; + + if (Array.isArray(itemOrItems)) { + return itemOrItems.map((item) => processItem(item)); + } else { + return processItem(itemOrItems); + } + } + + public async getItems(limit: number, skip: number, sort: string): Promise<{ data: Item[]; next: boolean }> { + let mediaFiles = await MediaFile.find({ + where: {type: this.type as "image"}, + limit: limit, + skip: skip, + sort: sort//@ts-ignore + }).populate('children', {sort: sort}) + + let next: number = (await MediaFile.count({ + where: {type: this.type as "image"}, + limit: limit, + skip: skip === 0 ? limit : skip + limit, + sort: sort + })) + + return { + data: this.MF2Item(mediaFiles), + next: !!next + } + } + + public async search(s: string): Promise{ + return []; + // return await MediaFile.find({ + // where: {filename: {contains: s}, mimeType: { contains: this.type}, parent: null}, + // sort: 'createdAt DESC' + // }).populate('children', {sort: 'createdAt DESC'}) + } + + public async upload(file: UploaderFile, filename: string, origFileName: string, imageSizes?: imageSizes | {}): Promise { + + let parent = await MediaFile.create(this.Item2MF({ + parent: null, + mimeType: file.type, + size: file.size, + path: file.fd, + cropType: 'origin', + filename: origFileName, + image_size: null, + url: `/${this.path}/${filename}` + })).fetch(); + + if (Object.keys(imageSizes).length) { + await this.createSizes(file, parent, filename, imageSizes) + } + + return this.MF2Item( await MediaFile.findOne({ + where: {id: parent.id} + })); + } + + public async getChildren(id: string): Promise { + return []; + return (await MediaFile.findOne({ + where: {id: id} + }).populate('children', {sort: 'createdAt DESC'})).children + } + + protected async createSizes(file: UploaderFile, parent: Item, filename: string, imageSizes: imageSizes): Promise { + console.log("SIZES") + // for (const sizeKey of Object.keys(imageSizes)) { + // let sizeName = randomFileName(filename, sizeKey, false) + + // let {width, height} = imageSizes[sizeKey] + + // if (parent.image_size.width < width || parent.image_size.height < height) continue + + // let newFile = await this.resizeImage(file.fd, `${this.dir}${sizeName}`, width, height) + // await MediaFile.create(this.Item2MF({ + // parent: parent.id, + // mimeType: parent.mimeType, + // size: newFile.size, + // filename: parent.filename, + // path: `${this.dir}${sizeName}`, + // cropType: sizeKey, + // url: `/${this.path}/${sizeName}` + // })) + // } + } + + protected async createThumb(id: string, file: UploaderFile, filename: string, origFileName: string): Promise { + // const thumbName = randomFileName(filename, 'thumb', false) + // const thumb = await this.resizeImage(file.fd, `${this.dir}${thumbName}`, 150, 150) + + // await MediaFile.create({ + // parent: id, + // mimeType: file.type, + // size: thumb.size, + // cropType: 'thumb', + // path: `${this.dir}${thumbName}`, + // filename: origFileName, + // image_size: sizeOf(`${this.dir}${thumbName}`), + // url: `/${this.path}/${thumbName}` + // }) + } + + protected async createEmptyMeta(id: string) { + //create empty meta + // let metaData: Meta = { + // author: "", + // description: "", + // title: "" + // } + + // for (const key of Object.keys(metaData)) { + // await sails.models[this.metaModel].create({ + // key: key, + // value: metaData[key], + // parent: id + // }) + // } + } + + public async getMeta(id: string): Promise<{ key: string, value: string }[]> { + return [] + return (await MediaFile.findOne(id).populate('meta')).meta + } + + async setMeta(id: string, data: { [p: string]: string }): Promise<{ msg: "success" }> { + // for (const key of Object.keys(data)) { + // await sails.models[this.metaModel].update({parent: id, key: key}, {value: data[key]}) + // } + return {msg: "success"} + } + + protected async resizeImage(input: string, output: string, width: number, height: number) { + return await sharp(input) + .resize({width: width, height: height}) + .toFile(output) + } + + public async uploadCropped(parent: Item, file: UploaderFile, filename: string, config: { + width: number, + height: number + }): Promise { + return null + // return await MediaFile.create({ + // parent: parent.id, + // mimeType: file.type, + // size: file.size, + // path: file.fd, + // cropType: `${config.width}x${config.height}`, + // filename: parent.filename, + // image_size: sizeOf(file.fd), + // url: `/${this.path}/${filename}` + // }).fetch() + } + + async delete(id: string): Promise { + await MediaFile.destroy({where: {id: id}}).fetch() + } +} \ No newline at end of file diff --git a/libs/adminpanel/ProductMediaManager/ProductMediaManager.ts b/libs/adminpanel/ProductMediaManager/ProductMediaManager.ts index 4bf5da47..2248184f 100644 --- a/libs/adminpanel/ProductMediaManager/ProductMediaManager.ts +++ b/libs/adminpanel/ProductMediaManager/ProductMediaManager.ts @@ -1,5 +1,6 @@ -import {AbstractMediaManager, Item, File, WidgetItem, Data, widgetJSON} from "./AbstractMediaManager"; -import {ApplicationItem, ImageItem, TextItem, VideoItem} from "./Items"; +import { SelectedMediaFileRecord } from "../../../models/SelectedMediaFile"; +import {AbstractMediaManager, Item, File, MediaManagerWidgetItem, Data, MediaManagerWidgetJSON} from "sails-adminpanel/lib/media-manager/AbstractMediaManager"; +import {ImageItem} from "./Items"; export class DefaultMediaManager extends AbstractMediaManager { public readonly itemTypes: File[] = []; @@ -7,25 +8,22 @@ export class DefaultMediaManager extends AbstractMediaManager { constructor(id: string, path: string, dir: string, model: string, metaModel: string, modelAssoc: string) { super(id, path, dir, model, modelAssoc); this.itemTypes.push(new ImageItem(path, dir, model, metaModel)) - this.itemTypes.push(new TextItem(path, dir, model, metaModel)) - this.itemTypes.push(new ApplicationItem(path, dir, model, metaModel)) - this.itemTypes.push(new VideoItem(path, dir, model, metaModel)) } public async getAll(limit: number, skip: number, sort: string): Promise<{ data: Item[], next: boolean }> { - let data: Item[] = await sails.models[this.model].find({ - where: {parent: null}, + let data: Item[] = await MediaFile.find({ + where: {}, limit: limit, skip: skip, sort: sort//@ts-ignore }).populate('children', {sort: sort}) - let next: number = (await sails.models[this.model].find({ - where: {parent: null}, + let next: number = await MediaFile.count({ + where: {}, limit: limit, skip: skip === 0 ? limit : skip + limit, sort: sort - })).length + }) return { data: data, @@ -34,42 +32,41 @@ export class DefaultMediaManager extends AbstractMediaManager { } public async searchAll(s: string): Promise { - return await sails.models[this.model].find({ - where: {filename: {contains: s}, parent: null}, - sort: 'createdAt DESC' - }).populate('children', {sort: 'createdAt DESC'}) + return [] + // return await MediaFile.find({ + // where: {filename: {contains: s}, parent: null}, + // sort: 'createdAt DESC' + // }).populate('children', {sort: 'createdAt DESC'}) } public async saveRelations(data: Data, model: string, modelId: string, modelAttribute: string): Promise { - let widgetItems: WidgetItem[] = [] + let widgetItems: MediaManagerWidgetItem[] = [] for (const [key, widgetItem] of data.list.entries()) { - let record = await sails.models[this.modelAssoc].create({ - mediaManagerId: this.id, - model: model, - modelId: modelId, - file: widgetItem.id, - sortOrder: key + 1, - }).fetch() - widgetItems.push({ - id: record.id as string, - }) + let init: Record = {} + init[`mediafile_${model}`] = widgetItem.id + init[model] = modelId + init["sortOrder"] = key + 1 + let record = await SelectedMediaFile.create(init).fetch() + // widgetItems.push({ + // id: record.id as string, + // }) } - let updateData: { [key: string]: widgetJSON } = {} + // let updateData: { [key: string]: MediaManagerWidgetJSON } = {} - updateData[modelAttribute] = {list: widgetItems, mediaManagerId: this.id} + // updateData[modelAttribute] = {list: widgetItems, mediaManagerId: this.id} - await sails.models[model].update({id: modelId}, updateData) + // await MediaFile.update({id: modelId}, updateData) } - public async getRelations(items: WidgetItem[]): Promise { - interface widgetItemVUE extends WidgetItem { + public async getRelations(items: MediaManagerWidgetItem[]): Promise { + interface widgetItemVUE extends MediaManagerWidgetItem { children: Item[] } let widgetItems: widgetItemVUE[] = [] for (const item of items) { - let record = (await sails.models[this.modelAssoc].find({where: {id: item.id}}))[0] - let file = (await sails.models[this.model].find({where: {id: record.file}}).populate('children', {sort: 'createdAt DESC'}))[0] as Item + let record = (await SelectedMediaFile.find({where: {id: item.id}}))[0] + let file = (await MediaFile.find({where: {id: record.file}}).populate('children', {sort: 'createdAt DESC'}))[0] as Item widgetItems.push({ id: file.id, children: file.children, @@ -84,9 +81,9 @@ export class DefaultMediaManager extends AbstractMediaManager { } public async deleteRelations(model: string, modelId: string): Promise{ - let modelAssociations = await sails.models[this.modelAssoc].find({where: {modelId: modelId, model: model}}) + let modelAssociations = await SelectedMediaFile.find({where: {modelId: modelId, model: model}}) for (const modelAssociation of modelAssociations) { - await sails.models[this.modelAssoc].destroy(modelAssociation.id).fetch() + await SelectedMediaFile.destroy(modelAssociation.id).fetch() } } }