Просмотр исходного кода

Media box: Get mime type from blob info

Danilo Bargen 7 лет назад
Родитель
Сommit
f2f35e43da
3 измененных файлов с 19 добавлено и 4 удалено
  1. 3 2
      src/directives/mediabox.ts
  2. 12 1
      src/filters.ts
  3. 4 1
      src/services/mediabox.ts

+ 3 - 2
src/directives/mediabox.ts

@@ -53,10 +53,11 @@ export default [
                 };
                 };
 
 
                 // Listen to Mediabox service events
                 // Listen to Mediabox service events
-                const filter = $filter('bufferToUrl') as (buffer: ArrayBuffer, mimeType: string) => string;
+                const bufferToUrl = $filter('bufferToUrl') as
+                    (buffer: ArrayBuffer, mimeType: string, trust: boolean) => string;
                 mediaboxService.evtMediaChanged.attach((dataAvailable: boolean) => {
                 mediaboxService.evtMediaChanged.attach((dataAvailable: boolean) => {
                     $rootScope.$apply(() => {
                     $rootScope.$apply(() => {
-                        this.imageDataUrl = filter(mediaboxService.data, 'image/jpeg');
+                        this.imageDataUrl = bufferToUrl(mediaboxService.data, mediaboxService.mimetype, true);
                         this.caption = mediaboxService.caption || mediaboxService.filename;
                         this.caption = mediaboxService.caption || mediaboxService.filename;
                     });
                     });
                 });
                 });

+ 12 - 1
src/filters.ts

@@ -237,7 +237,18 @@ angular.module('3ema.filters', [])
         for (let i = 0; i < len; i++) {
         for (let i = 0; i < len; i++) {
             binary += String.fromCharCode(bytes[i]);
             binary += String.fromCharCode(bytes[i]);
         }
         }
-        const uri = 'data:' + mimeType + ';base64,' +  btoa(binary);
+        switch (mimeType) {
+            case 'image/jpeg':
+            case 'image/png':
+            case 'image/webp':
+                // OK
+                break;
+            default:
+                $log.warn(logTag, 'Unknown mimeType: ' + mimeType);
+                mimeType = 'image/jpeg';
+                break;
+        }
+        const uri = 'data:' + mimeType + ';base64,' + btoa(binary);
         if (trust) {
         if (trust) {
             return $sce.trustAsResourceUrl(uri);
             return $sce.trustAsResourceUrl(uri);
         } else {
         } else {

+ 4 - 1
src/services/mediabox.ts

@@ -39,6 +39,7 @@ export class MediaboxService {
     public data: ArrayBuffer | null = null;
     public data: ArrayBuffer | null = null;
     public caption: string = '';
     public caption: string = '';
     public filename: string = '';
     public filename: string = '';
+    public mimetype: string = '';
 
 
     public static $inject = ['$log'];
     public static $inject = ['$log'];
     constructor($log: ng.ILogService) {
     constructor($log: ng.ILogService) {
@@ -48,10 +49,11 @@ export class MediaboxService {
     /**
     /**
      * Update media data.
      * Update media data.
      */
      */
-    public setMedia(data: ArrayBuffer, filename: string, caption: string) {
+    public setMedia(data: ArrayBuffer, filename: string, mimetype: string, caption: string) {
         this.$log.debug(this.logTag, 'Media data updated');
         this.$log.debug(this.logTag, 'Media data updated');
         this.data = data;
         this.data = data;
         this.filename = filename;
         this.filename = filename;
+        this.mimetype = mimetype;
         this.caption = caption;
         this.caption = caption;
         this.evtMediaChanged.post(data !== null);
         this.evtMediaChanged.post(data !== null);
     }
     }
@@ -63,6 +65,7 @@ export class MediaboxService {
         this.$log.debug(this.logTag, 'Media data cleared');
         this.$log.debug(this.logTag, 'Media data cleared');
         this.data = null;
         this.data = null;
         this.filename = '';
         this.filename = '';
+        this.mimetype = '';
         this.caption = '';
         this.caption = '';
         this.evtMediaChanged.post(false);
         this.evtMediaChanged.post(false);
     }
     }