@flumens/models 0.12.3 → 0.12.4
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/dist/Indicia/Media.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),i=require("axios"),a=require("@flumens/utils"),r=require("../Model.js")
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),i=require("axios"),a=require("@flumens/utils"),r=require("../Model.js");require("lodash");var s=require("@flumens/utils/dist/image");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(i);const d=a.isPlatform("hybrid");class h extends r.default{static getDataURI(e,t={}){const i=new Promise(((i,a)=>{if("string"==typeof e){let a=e.replace(/.*\.([a-z]+)$/i,"$1");return"jpg"===a&&(a="jpeg"),void h.resize(e,a,t.width,t.height).then((e=>{const[t,r]=e;i([r,a,t.width,t.height])}))}if(!window.FileReader)return void a(new Error("No File Reader"));const r=new FileReader;r.onload=function(a){var r,s;if(t.width||t.height)h.resize(null===(r=a.target)||void 0===r?void 0:r.result,e.type,t.width,t.height).then((t=>{const[a,r]=t;i([r,e.type,a.width,a.height])}));else{const t=new window.Image;t.onload=()=>{var r;const s=e.type.replace(/.*\/([a-z]+)$/i,"$1");i([null===(r=a.target)||void 0===r?void 0:r.result,s,t.width,t.height])},t.src=null===(s=a.target)||void 0===s?void 0:s.result}},r.readAsDataURL(e)}));return i}static resize(e,t,i,a){const r=new Promise((r=>{const s=new window.Image;s.onload=()=>{var e;let{width:n}=s,{height:o}=s;let d=null;d=n>o?n/(!i||i>n?n:i):o/(!a||a>o?o:a),n/=d,o/=d;const h=document.createElement("canvas");h.width=n,h.height=o,null===(e=h.getContext("2d"))||void 0===e||e.drawImage(s,0,0,n,o),r([s,h.toDataURL(t)])},s.src=e}));return r}static getImageModel(t,i,r){return e.__awaiter(this,void 0,void 0,(function*(){const e=this;if(!t)throw new Error("File not found while creating image model.");let s,n,o;if(d){const e=yield a.createImage(t);s=e.width,n=e.height,o=t.split("/").pop()}else[o,,s,n]=yield e.getDataURI(t,{width:2e3,height:2e3});const h=new e({data:{data:o,type:"jpeg",width:s,height:n,path:i}});return r||(yield h.addThumbnail()),h}))}constructor(i={}){var{metadata:a={}}=i,r=e.__rest(i,["metadata"]);r.createdAt=r.createdAt||(null==a?void 0:a.createdOn),r.updatedAt=r.updatedAt||(null==a?void 0:a.updatedOn),r.syncedAt=r.syncedAt||(null==a?void 0:a.syncedOn),super(r),this.debouncedValue=300,this.parent=this.parent,this.remote=t.observable({synchronising:!1,url:r.url,getAccessToken:r.getAccessToken}),this.metadata=t.observable(a)}toJSON(){const e=super.toJSON();return JSON.parse(JSON.stringify(Object.assign(Object.assign({},e),{metadata:t.toJS(this.metadata)||{}})))}setUpdatedAtTimestamp(e){var t;super.setUpdatedAtTimestamp(e),null===(t=this.parent)||void 0===t||t.setUpdatedAtTimestamp(e)}toDTO(e={}){const t=e[this.cid];if(!this.id&&!t)throw new Error("Image ID or queued ID is missing.");const i={values:{caption:this.data.caption}};return this.id?i.values.id=this.id:i.values.queued=t.name,i}sync(){const t=Object.create(null,{sync:{get:()=>super.sync}});return e.__awaiter(this,void 0,void 0,(function*(){return this.parent?this.parent.sync():t.sync.call(this)}))}uploadFile(){return e.__awaiter(this,arguments,void 0,(function*(e=!1){var t;if(this.id)throw new Error("A file of a media on the remote cannot be uploaded again.");const i=Date.now()-432e5,a=this.syncedAt>i&&this.data.queued;if(!e&&a)return;const r=yield this.getFormData(),s=new FormData;if(s.append(...r),!this.remote.getAccessToken)throw new Error("this.remote.getAccessToken is missing");const n={Authorization:`Bearer ${yield this.remote.getAccessToken()}`},d=yield o.default.post(`${this.remote.url}/index.php/services/rest/media-queue`,s,{headers:n,timeout:12e4});if(!(null===(t=d.data[this.cid])||void 0===t?void 0:t.name))throw new Error("New remote media name was not be found.");this.data.queued=d.data[this.cid].name,this.syncedAt=(new Date).getTime()}))}getFormData(){return e.__awaiter(this,void 0,void 0,(function*(){const{type:e}=this.data;let t=e,i=e;(null==e?void 0:e.match(/image.*/))?[,t]=e.split("/"):i=`image/${i}`;const a=this.getURL(),r=yield s.getBlobFromURL(a,i),n=this.cid;return[n,r,`${n}.${t}`]}))}getRemoteURL(){if(!this.remote.url)throw new Error("No remote url was set.");if(!this.data.queued&&!this.data.path)throw new Error("No media queued or path attribute.");const e=this.remote.url.replace("/index.php/services/rest","");return this.data.queued?`${e}/upload-queue/${this.data.queued}`:`${e}/upload/${this.data.path}`}getURL(){return this.data.data}resize(e,t){const i=this,a=new Promise(((a,r)=>{h.resize(this.getURL(),this.data.type,e,t).then((e=>{const[t,r]=e;i.data.data=r,a([t,r])})).catch(r)}));return a}addThumbnail(e={}){const t=this;return new Promise(((e,i)=>{/^data:/i.test(this.getURL())?h.resize(this.getURL(),this.data.type,100,100).then((i=>{const[,a]=i;t.data.thumbnail=a,e()})).catch(i):h.getDataURI(this.getURL(),{width:100,height:100}).then((i=>{[t.data.thumbnail]=i,e()})).catch(i)}))}get isUploaded(){if(!this.parent)throw new Error("No media parent to return disabled status.");return this.parent.isUploaded}get isDisabled(){return this.isUploaded}get isSynchronising(){return this.remote.synchronising}}exports.default=h;
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
+
export { dataURItoBlob, isDataURL, getBlobFromURL, } from '@flumens/utils/dist/image';
|
|
1
2
|
export declare const getErrorMessageFromObject: (errors: any) => string;
|
|
2
|
-
/**
|
|
3
|
-
* Converts DataURI object to a Blob.
|
|
4
|
-
*
|
|
5
|
-
* @param {type} dataURI
|
|
6
|
-
* @param {type} fileType
|
|
7
|
-
* @returns {undefined}
|
|
8
|
-
*/
|
|
9
|
-
export declare function dataURItoBlob(dataURI: string, fileType: string): Blob;
|
|
10
|
-
export declare function isDataURL(string: string): boolean;
|
|
11
|
-
export declare function getBlobFromURL(url: string, mediaType: string): Promise<unknown>;
|
|
12
3
|
export interface RemoteConfig {
|
|
13
4
|
id?: number | string;
|
|
14
5
|
values?: {
|
|
@@ -37,4 +28,3 @@ export declare const getCamelCaseObj: (dto: any) => import("lodash").Dictionary<
|
|
|
37
28
|
export declare const parseRemoteAttrs: (config: {
|
|
38
29
|
[key: string]: AttrConfig;
|
|
39
30
|
}, remoteAttrs: RemoteAttr[]) => any;
|
|
40
|
-
export {};
|
package/dist/Indicia/helpers.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash")
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash"),t=require("@flumens/utils/dist/image");Object.defineProperty(exports,"dataURItoBlob",{enumerable:!0,get:function(){return t.dataURItoBlob}}),Object.defineProperty(exports,"getBlobFromURL",{enumerable:!0,get:function(){return t.getBlobFromURL}}),Object.defineProperty(exports,"isDataURL",{enumerable:!0,get:function(){return t.isDataURL}}),exports.getCamelCaseObj=t=>e.mapKeys(t,((t,r)=>e.camelCase(r))),exports.getErrorMessageFromObject=e=>Object.entries(e).reduce(((e,t)=>`${e}${t[0]} ${t[1]}\n`),""),exports.parseRemoteAttrs=(e,t)=>{e=e||{};const r=Object.entries(e);return t.reduce(((e,t)=>{const s=r.find((([,e])=>{var r;return(null===(r=e.remote)||void 0===r?void 0:r.id)===parseInt(t.id,10)}));if(!s)return e;const[i]=s,n=isNaN(t.value)?t.value:parseFloat(t.value);return e[i]=n,e}),{})},exports.validateRemoteModel=function(){const e=this.getSurvey(),t=e.verify&&e.verify(this.data,this);let r={};if(t){const e=t&&"issues"in t;!("ValidationError"===(null==t?void 0:t.name)||"ValidationError"===(null==t?void 0:t.type))&&!e?r.errors=[t.message]:e?r.errors=t.issues.map((({message:e})=>e)):r=Object.assign({},t)}const s=(e,t)=>{const r=t.validateRemote();return r&&(e[t.cid]=r,e[t.cid].model=t),e},i=(this.samples||[]).reduce(s,{}),n=(this.occurrences||[]).reduce(s,{}),o=(this.media||[]).reduce(s,{});if(Object.keys(r).length||Object.keys(i).length||Object.keys(n).length||Object.keys(o).length){return{attributes:r,model:this,models:Object.assign(Object.assign(Object.assign({},i),n),o)}}return null};
|