mikro-orm-attachments 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/converters/ImgkitConverter.js +1 -1
- package/dist/converters/SharpConverter.js +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -1
- package/dist/metadata/BaseMetadata.js +1 -1
- package/dist/metadata/ImgkitMetadata.js +1 -1
- package/dist/metadata/SharpMetadata.js +1 -1
- package/dist/shared/chunk-tgvybtjr.js +2 -0
- package/dist/shared/chunk-wrbx2tbm.js +2 -0
- package/package.json +6 -7
- package/dist/shared/chunk-523ncfdc.js +0 -2
- package/dist/shared/chunk-cm81a68q.js +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [2.1.1](https://github.com/reptoxx/mikro-orm-attachments/compare/mikro-orm-attachments@2.1.0...mikro-orm-attachments@2.1.1) (2026-04-08)
|
|
6
|
+
|
|
7
|
+
## [2.1.0](https://github.com/reptoxx/mikro-orm-attachments/compare/mikro-orm-attachments@2.0.0...mikro-orm-attachments@2.1.0) (2026-04-08)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### ⚠ BREAKING CHANGES
|
|
11
|
+
|
|
12
|
+
* only support v7
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* only support v7 ([4397990](https://github.com/reptoxx/mikro-orm-attachments/commit/4397990322df92c85c4c77b023f7e6e4072d0028))
|
|
17
|
+
|
|
5
18
|
## [2.0.0](https://github.com/reptoxx/mikro-orm-attachments/compare/mikro-orm-attachments@1.3.2...mikro-orm-attachments@2.0.0) (2026-04-07)
|
|
6
19
|
|
|
7
20
|
### [1.3.2](https://github.com/reptoxx/mikro-orm-attachments/compare/mikro-orm-attachments@1.3.1...mikro-orm-attachments@1.3.2) (2026-02-24)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as o}from"../shared/chunk-4thj50nc.js";import{b as i}from"../shared/chunk-
|
|
1
|
+
import{a as o}from"../shared/chunk-4thj50nc.js";import{b as i}from"../shared/chunk-tgvybtjr.js";import{fileTypeFromBuffer as n}from"file-type";class m extends o{options;constructor(t){super();this.options=t}async supports(t){return t.mimeType.startsWith("image/")}async handle(t){let e=await(await i("imgkit")).transform(t.buffer,this.options??{}),r=await n(new Uint8Array(e.buffer));return{buffer:e,mimeType:r?.mime??t.mimeType,extname:r?.ext??t.extname}}}export{m as ImgkitConverter};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as n}from"../shared/chunk-4thj50nc.js";import{b as a}from"../shared/chunk-
|
|
1
|
+
import{a as n}from"../shared/chunk-4thj50nc.js";import{b as a}from"../shared/chunk-tgvybtjr.js";import{fileTypeFromBuffer as h}from"file-type";class u extends n{options;constructor(t){super();this.options=t}async supports(t){return t.mimeType.startsWith("image/")}async handle(t){let s=await a("sharp"),f=this.options?.resize||{},e=this.options?.format||"webp",m=this.options?.autoOrient||!0,r={};if(typeof e!=="string")r=e?.options,e=e.format;let o=s(t.buffer).withMetadata();if(m)o.autoOrient();let p=await o.resize(f).toFormat(e,r).toBuffer(),i=await h(p);return{buffer:p,mimeType:i?.mime??t.mimeType,extname:i?.ext??t.extname}}}export{u as SharpConverter};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as
|
|
1
|
+
import{a as B}from"./shared/chunk-4thj50nc.js";import{c as C}from"./shared/chunk-tgvybtjr.js";import{Buffer as z}from"node:buffer";var x=Symbol("attachment:loaded"),M=Symbol("attachment:options"),y=Symbol("attachment:file"),m=Symbol("attachment:disk"),d=Symbol("attachment:fn:process"),P=Symbol("attachment:fn:load"),O=Symbol("attachment:fn:save");class p{[x]=!1;[y];[m];data;constructor(t){if(t instanceof File)this[y]=t;else this[x]=!0,this.data=t}#t(){if(!this[x])throw Error("Attachment is not processed, please flush the entity first.")}get#s(){return this.#t(),this[m]}[O](){return this.data??{}}static[P](t){return new p(t)}[d](t){if(this[x])return;this.data=t,this[x]=!0}static fromFile(t){return new p(t)}static async fromUrl(t){let r=await fetch(t),s=await r.arrayBuffer(),o,e=r.headers.get("content-disposition");if(e){let S=e.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);if(S)o=S[1].replace(/['"]/g,"")}if(!o)o=t.split("?")[0].split("/").pop()||"download";let n=z.from(s),c=new File([n],o,{type:r.headers.get("content-type")??""});return new p(c)}#r(t){this.#t();let r=this.data?.variants.find((s)=>s.name===t)??null;if(!r)throw Error(`Attachment: Variant '${t}' not found`);return r}url(t){if(this.#t(),t)return this.#s?.getUrl(this.#r(t).path);return this.data?.url}originalName(){return this.data?.originalName??""}size(t){if(t)return this.#r(t).size;return this.data?.size}meta(t){if(t)return this.#r(t).meta;return this.data?.meta}preSignedUrl(t,r){if(this.#t(),typeof t==="string")return this.#s?.getSignedUrl(this.#r(t).path,r);return this.#s?.getSignedUrl(this.data?.path??"",r)}blurhash(){return this.#t(),this.data?.blurhash}getDisk(){return this.#s}getDrive(){return this.data?.drive??""}async getBytes(t){this.#t();let r=t?this.#r(t).path:this.data?.path??"";return this.#s?.getBytes(r)}async getBuffer(t){this.#t();let r=await this.getBytes(t);if(!r)return null;return z.from(r)}async getStream(t){this.#t();let r=t?this.#r(t).path:this.data?.path??"";return this.#s?.getStream(r)}getMimeType(t){return this.#t(),(t?this.#r(t):this.data)?.mimeType??""}toJSON(){return{url:this.url(),blurhash:this.blurhash()}}}import{Disk as A}from"flydrive";import{join as w}from"node:path";import{fileTypeFromBuffer as I}from"file-type";import{v7 as Q}from"uuid";class l{att;file;disk;buffer;modelOptions;config;entity;columnName;diskName;fileInfo;constructor(t,r){this.att=t;let{disk:s,options:o,config:e,entity:n,columnName:c,diskName:i}=r;if(this.disk=s,this.modelOptions=o,this.config=e,this.entity=n,this.columnName=c,this.diskName=i,t[x])throw Error("Attachment already processed, please use the Attachment.fromFile method to create a new attachment");this.file=this.att[y]}done(t){this.att[d](t)}generateKey(t,...r){let s=this.modelOptions.folder??"";return s=s.replace(/:([A-Za-z0-9_]+)/g,(o,e)=>{let n=this.entity[e];if(n===void 0||n===null)throw Error(`Missing value for Attachment path "${e}" in entity ${this.entity.constructor.name}. Please ensure that the referenced property is computed before the attachment is processed. (Auto incrementing fields are not supported.)`);return this.normalizeFileName(String(n))}),w(s,t,...r)}normalizeFileName(t){return encodeURIComponent(t.replace(/[^a-zA-Z0-9.-]/g,"_")).toLowerCase()}async fileToBuffer(){if(this.buffer)return this.buffer;let t=await this.file.arrayBuffer(),r=Buffer.from(t);return this.buffer=r,r}getFileName(){if(!this.config.rename)return this.normalizeFileName(this.file.name);if(typeof this.config.rename==="function")return this.normalizeFileName(this.config.rename(this.file,this.columnName,this.entity));return this.normalizeFileName(Q())}async getFileType(){return I(await this.fileToBuffer())}async analyseFile(){let t=await this.getFileType();this.fileInfo={extname:t?.ext??this.file.name.split(".").pop()??"",mimeType:t?.mime??this.file.type,size:this.file.size}}async uploadFile(t,r){await this.disk.put(t,r)}async pickConverter(t,r){if(!this.fileInfo)throw Error("Attachment Converter: File info not found");if(this.config.variants?.[t])r=this.config.variants[t];if(r instanceof B){if(await r.supports({size:this.fileInfo.size,mimeType:this.fileInfo.mimeType,buffer:await this.fileToBuffer(),extname:this.fileInfo.extname}))return r}return null}async process(){await this.analyseFile();let t=await this.fileToBuffer(),r=this.getFileName(),s=this.fileInfo?.extname??"",o=this.generateKey(r,`${r}.${s}`);await this.uploadFile(o,t);let e={name:r,extname:s,size:this.file.size,drive:this.diskName,mimeType:this.file.type,path:o,url:await this.disk.getUrl(o),originalName:this.file.name,variants:[]},n=await this.config.metadata?.metadata({buffer:t,size:this.fileInfo?.size??0,mimeType:this.fileInfo?.mimeType??"",extname:this.fileInfo?.extname??""});if(e.meta=n,this.modelOptions.variants)for(let[c,i]of Object.entries(this.modelOptions.variants)){let S=await this.pickConverter(c,i);if(!S)throw Error(`Attachment Converter: No converter for the variant ${c} found`);let h=await S.handle({buffer:t,size:this.fileInfo?.size??0,mimeType:this.fileInfo?.mimeType??"",extname:this.fileInfo?.extname??"",variantName:c,variant:i}),b=this.generateKey(o.split("/").pop().split(".").shift(),`${c}.${h.extname}`);await this.uploadFile(b,h.buffer),e.variants.push({name:c,extname:h.extname,size:h.buffer.length,mimeType:h.mimeType,path:b})}if(this.fileInfo?.mimeType.startsWith("image/")){if(typeof this.modelOptions.blurhash==="boolean"?this.modelOptions.blurhash:this.modelOptions.blurhash?.enabled)e.blurhash=await C(t,typeof this.modelOptions.blurhash==="object"?this.modelOptions.blurhash:void 0)}this.done(e)}}import{Property as Z}from"@mikro-orm/decorators/legacy";import{Type as X,ValidationError as J}from"@mikro-orm/core";class V extends X{options={folder:"attachments",blurhash:!0};constructor(t){super();this.options={...this.options,...t}}convertToDatabaseValue(t,r){if(t===null)return null;if(t instanceof p)return t[O]();throw J.invalidType(V,t,"js")}convertToJSValue(t){try{return p[P](JSON.parse(t))}catch(r){try{return p[P](t)}catch(s){throw J.invalidType(V,t,"database")}}}getColumnType(){return"json"}}var T={blurhash:!0},F={rename:!0},W=["accessor","comment","customOrder","getter","getterName","hidden","index","lazy","name","nullable","serializedName","serializedPrimaryKey","serializer","version"];var R=Symbol("attachment:props");function $(t={}){return(r,s)=>{let{mikro:o,attachment:e}=G(t,W);e={...T,...e};let n=r.constructor;if(!n[R])n[R]={};return n[R][s]=e,Z({...o,type:new V(e)})(r,s)}}function Y(t){return $(t)}function g(t){let s=t.constructor[R];return s?s:{}}function G(t,r){let s=new Set(r),o={},e={};for(let n in t)if(s.has(n))o[n]=t[n];else e[n]=t[n];return{mikro:o,attachment:e}}class q{options;disks;constructor(t){this.options=t;this.options={...F,...this.options},this.disks=new Map(Object.entries(this.options.drivers).map(([r,s])=>[r,new A(s)]))}async onLoad(t){let{entity:r}=t,s=g(r);for(let o of Object.keys(s)){let e=r[o],n=s[o];if(e instanceof p){let c=this.#t(n,e.getDrive(),!1);if(!c)continue;e[m]=c}}}#t(t,r,s){let o=this.disks.get(r??t.driver??this.options.defaultDriver);if(r&&!o)o=this.disks.get(t.driver??this.options.defaultDriver);if(!o){if(s)throw Error(`Unknown attachment driver "${String(r??t.driver??this.options.defaultDriver)}"`);return null}return o}async beforeFlush(t){let{uow:r}=t,s=new Set([...r.getChangeSets().map((o)=>o.entity),...r.getPersistStack()]);for(let o of s)await this.#s(o)}async#s(t){let r=g(t);for(let s of Object.keys(r)){let o=t[s],e=r[s];if(o instanceof p){if(o[x])continue;let n=this.#t(e);if(!n)continue;o[m]=n;let c=this.#r(e.variants),i=new l(o,{disk:n,options:{...e,variants:c},config:this.options,entity:t,columnName:s,diskName:e.driver??this.options.defaultDriver});try{await i.process()}catch(S){throw console.error(S),Error("Failed to process attachment")}}}}AttachmentDecorator(t){return Y(t)}#r(t){if(!t)return;if(Array.isArray(t)){let r={};for(let s of t)if(typeof s==="string"){let o=this.options.variants?.[s];if(!o)throw Error(`Unknown attachment variant "${s}"`);r[s]=o}else if(s&&typeof s==="object")Object.assign(r,s);return r}if(t&&typeof t==="object")return t;return}}export{q as AttachmentSubscriber,p as Attachment};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{d as a}from"../shared/chunk-wrbx2tbm.js";export{a as BaseMetadata};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{b as t}from"../shared/chunk-
|
|
1
|
+
import{b as t}from"../shared/chunk-tgvybtjr.js";import{d as a}from"../shared/chunk-wrbx2tbm.js";class m extends a{async metadata(e){return await(await t("imgkit")).metadata(e.buffer)}}export{m as ImgkitMetadata};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import"../shared/chunk-
|
|
1
|
+
import"../shared/chunk-tgvybtjr.js";import"../shared/chunk-wrbx2tbm.js";
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createRequire as l}from"module";import{encode as h}from"blurhash";var m=l(import.meta.url);async function f(t){try{let r=await import(t);if(r.default)return r.default;return r}catch(r){throw Error(`Module ${t} not found, please install it first.`)}}function w(t,r){let{componentX:n,componentY:a}=r??{enabled:!0,componentX:4,componentY:4};return new Promise(async(i,s)=>{try{let e=await f("sharp"),{data:u,info:o}=await e(t).raw().ensureAlpha().toBuffer({resolveWithObject:!0}),c=h(new Uint8ClampedArray(u),o.width,o.height,n,a);return i(c)}catch(e){return s(e)}})}
|
|
2
|
+
export{f as b,w as c};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mikro-orm-attachments",
|
|
3
3
|
"description": "Attachment handling for MikroORM with support for image processing, variants, and multiple storage drivers",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -72,18 +72,17 @@
|
|
|
72
72
|
"type": "git",
|
|
73
73
|
"url": "https://github.com/reptoxx/mikro-orm-attachments"
|
|
74
74
|
},
|
|
75
|
-
"optionalDependencies": {
|
|
76
|
-
"@mikro-orm/decorators": "^7.0.9"
|
|
77
|
-
},
|
|
75
|
+
"optionalDependencies": {},
|
|
78
76
|
"peerDependencies": {
|
|
79
|
-
"@mikro-orm/core": "^7.0.
|
|
77
|
+
"@mikro-orm/core": "^7.0.0",
|
|
78
|
+
"@mikro-orm/decorators": "^7.0.0",
|
|
80
79
|
"sharp": "^0.34.5",
|
|
81
80
|
"blurhash": "^2.0.5",
|
|
82
81
|
"flydrive": "^2.1.0"
|
|
83
82
|
},
|
|
84
83
|
"devDependencies": {
|
|
85
84
|
"@mikro-orm/core": "^7.0.9",
|
|
86
|
-
"@typescript/native-preview": "^7.0.0-dev.
|
|
85
|
+
"@typescript/native-preview": "^7.0.0-dev.20260408.1",
|
|
87
86
|
"bun-types": "1.3.11",
|
|
88
87
|
"bunup": "^0.16.31",
|
|
89
88
|
"imgkit": "^2.2.1",
|
|
@@ -91,7 +90,7 @@
|
|
|
91
90
|
"typescript": "^6.0.2"
|
|
92
91
|
},
|
|
93
92
|
"dependencies": {
|
|
94
|
-
"file-type": "^
|
|
93
|
+
"file-type": "^22.0.0",
|
|
95
94
|
"uuid": "^13.0.0"
|
|
96
95
|
},
|
|
97
96
|
"engines": {
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{createRequire as l}from"module";import{encode as f}from"blurhash";var h=l(import.meta.url);async function p(t){try{let r=await import(t);if(r.default)return r.default;return r}catch(r){throw Error(`Module ${t} not found, please install it first.`)}}function w(t){for(let r of t)try{return h(r)}catch{continue}throw Error(`Could not resolve any of the following modules: ${t.join(", ")}`)}function y(t,r){let{componentX:n,componentY:a}=r??{enabled:!0,componentX:4,componentY:4};return new Promise(async(i,s)=>{try{let e=await p("sharp"),{data:u,info:o}=await e(t).raw().ensureAlpha().toBuffer({resolveWithObject:!0}),c=f(new Uint8ClampedArray(u),o.width,o.height,n,a);return i(c)}catch(e){return s(e)}})}
|
|
2
|
-
export{p as b,w as c,y as d};
|