@@ -15,8 +15,6 @@ import { Timestamp } from '../models';
1515import type { MimeType } from '../types' ;
1616import type { ClipType } from '../clips' ;
1717
18- type Url = string | URL | Request ;
19-
2018type Events = {
2119 load : undefined ;
2220 update : undefined ;
@@ -32,7 +30,7 @@ export class Source extends EventEmitterMixin<Events, typeof Serializer>(Seriali
3230 * Locally accessible blob address to the data
3331 */
3432 @serializable ( )
35- public objectURL : string | undefined ;
33+ public objectURL ? : string ;
3634
3735 /**
3836 * Defines the default duration
@@ -111,36 +109,44 @@ export class Source extends EventEmitterMixin<Events, typeof Serializer>(Seriali
111109 return this . file ;
112110 }
113111
114- public async from ( input : File | Url , init ?: RequestInit | undefined ) : Promise < this> {
112+ protected async loadFile ( file : File ) {
113+ this . name = file . name ;
114+ this . mimeType = parseMimeType ( file . type ) ;
115+ this . external = false ;
116+ this . file = file ;
117+ }
118+
119+ protected async loadUrl ( url : string | URL | Request , init ?: RequestInit ) {
120+ const res = await fetch ( url , init ) ;
121+
122+ if ( ! res ?. ok ) throw new IOError ( {
123+ code : 'unexpectedIOError' ,
124+ message : 'An unexpected error occurred while fetching the file' ,
125+ } ) ;
126+
127+ const blob = await res . blob ( ) ;
128+ this . name = url . toString ( ) . split ( '/' ) . at ( - 1 ) ?? '' ;
129+ this . external = true ;
130+ this . file = new File ( [ blob ] , this . name , { type : blob . type } ) ;
131+ this . externalURL = url ;
132+ this . mimeType = parseMimeType ( blob . type ) ;
133+ }
134+
135+ public async from ( input : File | string | URL | Request , init ?: RequestInit ) : Promise < this> {
115136 try {
116137 this . state = 'LOADING' ;
117138
118139 if ( input instanceof File ) {
119- this . name = input . name ;
120- this . mimeType = parseMimeType ( input . type ) ;
121- this . external = false ;
122- this . file = input ;
140+ await this . loadFile ( input ) ;
123141 } else {
124- // case input is a request url
125- const res = await fetch ( input , init ) ;
126-
127- if ( ! res ?. ok ) throw new IOError ( {
128- code : 'unexpectedIOError' ,
129- message : 'An unexpected error occurred while fetching the file' ,
130- } ) ;
131-
132- const blob = await res . blob ( ) ;
133- this . name = input . toString ( ) . split ( '/' ) . at ( - 1 ) ?? '' ;
134- this . external = true ;
135- this . file = new File ( [ blob ] , this . name , { type : blob . type } ) ;
136- this . externalURL = input ;
137- this . mimeType = parseMimeType ( blob . type ) ;
142+ await this . loadUrl ( input , init ) ;
138143 }
139144
140145 this . state = 'READY' ;
141146 this . trigger ( 'load' , undefined ) ;
142147 } catch ( e ) {
143148 this . state == 'ERROR' ;
149+ this . trigger ( 'error' , new Error ( String ( e ) ) ) ;
144150 throw e ;
145151 }
146152
@@ -173,7 +179,7 @@ export class Source extends EventEmitterMixin<Events, typeof Serializer>(Seriali
173179 /**
174180 * Downloads the file
175181 */
176- public async export ( ) : Promise < void > {
182+ public async download ( ) : Promise < void > {
177183 const file = await this . getFile ( ) ;
178184
179185 downloadObject ( file , this . name ) ;
@@ -192,7 +198,7 @@ export class Source extends EventEmitterMixin<Events, typeof Serializer>(Seriali
192198 */
193199 public static async from < T extends Source > (
194200 this : new ( ) => T ,
195- input : File | Url ,
201+ input : File | string | URL | Request ,
196202 init ?: RequestInit | undefined ,
197203 source = new this ( ) ,
198204 ) : Promise < T > {
0 commit comments