@@ -2,6 +2,8 @@ import { EventEmitter } from 'events';
2
2
import { generateApiKey } from 'generate-api-key' ;
3
3
import md5 from 'md5' ;
4
4
5
+ import { objType } from 'for-promise/utils/lib.mjs' ;
6
+
5
7
class BlobUrlManager extends EventEmitter {
6
8
// Constructor
7
9
constructor ( ) {
@@ -13,6 +15,8 @@ class BlobUrlManager extends EventEmitter {
13
15
this . queue = { } ;
14
16
this . ids = { } ;
15
17
this . mime = { } ;
18
+ this . size = { } ;
19
+ this . imgSize = { } ;
16
20
this . idsReverse = { } ;
17
21
}
18
22
@@ -46,6 +50,45 @@ class BlobUrlManager extends EventEmitter {
46
50
return null ;
47
51
}
48
52
53
+ getImgSize ( blobUrl ) {
54
+ if ( objType ( this . imgSize [ blobUrl ] , 'object' ) ) return this . imgSize [ blobUrl ] ;
55
+ return null ;
56
+ }
57
+
58
+ async forceGetImgSize ( blobUrl ) {
59
+ const file = this . getImgSize ( blobUrl ) ;
60
+ if ( objType ( file , 'object' ) ) {
61
+ if ( typeof file . height === 'number' && typeof file . width === 'number' ) return file ;
62
+ else return this . fetchImgSize ( blobUrl ) ;
63
+ }
64
+ return null ;
65
+ }
66
+
67
+ fetchImgSize ( blobUrl ) {
68
+ if ( objType ( this . imgSize [ blobUrl ] , 'object' ) ) {
69
+ const tinyThis = this ;
70
+ return new Promise ( ( resolve , reject ) => {
71
+ const img = new Image ( ) ;
72
+ img . onload = ( ) => {
73
+ if ( objType ( tinyThis . imgSize [ blobUrl ] , 'object' ) ) {
74
+ tinyThis . imgSize [ blobUrl ] = { height : img . height , width : img . width } ;
75
+ resolve ( tinyThis . imgSize [ blobUrl ] ) ;
76
+ } else {
77
+ reject ( new Error ( 'Invalid file url!' ) ) ;
78
+ }
79
+ } ;
80
+ img . onerror = reject ;
81
+ img . src = blobUrl ;
82
+ } ) ;
83
+ }
84
+ return null ;
85
+ }
86
+
87
+ getSize ( blobUrl ) {
88
+ if ( typeof this . size [ blobUrl ] === 'number' ) return this . size [ blobUrl ] ;
89
+ return null ;
90
+ }
91
+
49
92
async insert ( file , ops = { } ) {
50
93
// Insert using Hash
51
94
const tinyKey = generateApiKey ( ) ;
@@ -86,6 +129,12 @@ class BlobUrlManager extends EventEmitter {
86
129
// Mime
87
130
this . mime [ tinyUrl ] = typeof file . type === 'string' ? file . type . split ( '/' ) : [ ] ;
88
131
132
+ // Size
133
+ this . size [ tinyUrl ] = file . size ;
134
+
135
+ // Image Size
136
+ this . imgSize [ tinyUrl ] = { height : file . height , width : file . width } ;
137
+
89
138
// Hash
90
139
this . urls [ tinyUrl ] = hash ;
91
140
@@ -159,6 +208,8 @@ class BlobUrlManager extends EventEmitter {
159
208
}
160
209
161
210
delete this . mime [ tinyUrl ] ;
211
+ delete this . size [ tinyUrl ] ;
212
+ delete this . imgSize [ tinyUrl ] ;
162
213
delete this . hashes [ hash ] ;
163
214
delete this . timeout [ hash ] ;
164
215
delete this . urls [ tinyUrl ] ;
0 commit comments