Ver Fonte

Use file names as specified in blob response

Danilo Bargen há 7 anos atrás
pai
commit
c3eb03c8fb

+ 2 - 2
src/directives/message.ts

@@ -108,7 +108,7 @@ export default [
                 this.download = (ev) => {
                     this.downloading = true;
                     webClientService.requestBlob(this.message.id, this.receiver)
-                        .then((buffer: ArrayBuffer) => {
+                        .then((blobInfo: threema.BlobInfo) => {
                             $rootScope.$apply(() => {
                                 this.downloading = false;
 
@@ -117,7 +117,7 @@ export default [
                                     case 'video':
                                     case 'file':
                                     case 'audio':
-                                        saveAs(new Blob([buffer]), messageService.getFileName(this.message));
+                                        saveAs(new Blob([blobInfo.buffer]), blobInfo.filename);
                                         break;
                                     default:
                                         $log.warn('Ignored download request for message type', this.message.type);

+ 10 - 6
src/directives/message_media.ts

@@ -169,7 +169,7 @@ export default [
                     const receiver: threema.Receiver = this.receiver;
                     this.downloading = true;
                     webClientService.requestBlob(message.id, receiver)
-                        .then((buffer: ArrayBuffer) => {
+                        .then((blobInfo: threema.BlobInfo) => {
                             $rootScope.$apply(() => {
                                 this.downloading = false;
                                 this.downloaded = true;
@@ -177,24 +177,28 @@ export default [
                                 switch (this.message.type) {
                                     case 'image':
                                         const caption = message.caption || '';
-                                        mediaboxService.setMedia(buffer, messageService.getFileName(message), caption);
+                                        mediaboxService.setMedia(
+                                            blobInfo.buffer,
+                                            blobInfo.filename,
+                                            caption,
+                                        );
                                         break;
                                     case 'video':
-                                        saveAs(new Blob([buffer]), messageService.getFileName(message));
+                                        saveAs(new Blob([blobInfo.buffer]), blobInfo.filename);
                                         break;
                                     case 'file':
                                         if (this.message.file.type === 'image/gif') {
                                             // show inline
-                                            this.blobBuffer = buffer;
+                                            this.blobBuffer = blobInfo.buffer;
                                             // hide thumbnail
                                             this.showThumbnail = false;
                                         } else {
-                                            saveAs(new Blob([buffer]), messageService.getFileName(message));
+                                            saveAs(new Blob([blobInfo.buffer]), blobInfo.filename);
                                         }
                                         break;
                                     case 'audio':
                                         // Show inline
-                                        this.playAudio(buffer);
+                                        this.playAudio(blobInfo.buffer);
                                         break;
                                     default:
                                         $log.warn('Ignored download request for message type', this.message.type);

+ 0 - 43
src/services/message.ts

@@ -106,49 +106,6 @@ export class MessageService {
         return false;
     }
 
-    /**
-     * return the filename of a message (image, audio, file)
-     * used for downloads
-     * @param message
-     * @returns filename string or null
-     */
-    public getFileName(message: threema.Message): string {
-        if (message === undefined
-            || message === null) {
-            return null;
-        }
-
-        const getFileName = (prefix: string, postfix?: string): string => {
-            if (message.id === undefined) {
-                this.$log.warn('missing id on message model');
-                return null;
-            }
-            return prefix
-                + '-' + message.id
-                + (postfix !== undefined ? '.' + postfix : '');
-        };
-
-        switch (message.type) {
-            case 'image':
-                return getFileName('image', 'jpg');
-            case 'video':
-                return getFileName('video', 'mpg');
-            case 'file':
-                if (message.file !== undefined) {
-                    return message.file.name;
-                }
-
-                // should not happen
-                this.$log.warn('file message without file object', message.id);
-                return getFileName('file');
-            case 'audio':
-                return getFileName('audio', 'mp4');
-            default:
-                // ignore file types without a read file
-                return null;
-        }
-    }
-
     /**
      * Create a message object with a temporaryId
      */

+ 13 - 8
src/services/webclient.ts

@@ -109,6 +109,8 @@ export class WebClientService {
     private static DELETE_GROUP_TYPE_LEAVE = 'leave';
     private static DELETE_GROUP_TYPE_DELETE = 'delete';
     private static DATA_FIELD_BLOB_BLOB = 'blob';
+    private static DATA_FIELD_BLOB_TYPE = 'type';
+    private static DATA_FIELD_BLOB_NAME = 'name';
     private static DC_LABEL = 'THREEMA';
 
     private logTag: string = '[WebClientService]';
@@ -168,7 +170,7 @@ export class WebClientService {
     private trustedKeyStore: TrustedKeyStoreService;
     public version = null;
 
-    private blobCache = new Map<string, ArrayBuffer>();
+    private blobCache = new Map<string, threema.BlobInfo>();
     private loadingMessages = new Map<string, boolean>();
 
     public receiverListener: threema.ReceiverListener[] = [];
@@ -854,7 +856,7 @@ export class WebClientService {
     /**
      * Request a blob.
      */
-    public requestBlob(msgId: string, receiver: threema.Receiver): Promise<ArrayBuffer> {
+    public requestBlob(msgId: string, receiver: threema.Receiver): Promise<threema.BlobInfo> {
         const cached = this.blobCache.get(msgId + receiver.type);
 
         if (cached !== undefined) {
@@ -1801,7 +1803,7 @@ export class WebClientService {
         };
     }
 
-    private _receiveResponseBlob(message: threema.WireMessage): threema.PromiseRequestResult<ArrayBuffer> {
+    private _receiveResponseBlob(message: threema.WireMessage): threema.PromiseRequestResult<threema.BlobInfo> {
         this.$log.debug('Received blob response');
 
         // Unpack data and arguments
@@ -1830,20 +1832,23 @@ export class WebClientService {
         }
 
         // Unpack data
-        const buffer: ArrayBuffer = data[WebClientService.DATA_FIELD_BLOB_BLOB];
-        if (buffer === undefined) {
+        const blobInfo: threema.BlobInfo = {
+            buffer: data[WebClientService.DATA_FIELD_BLOB_BLOB],
+            mimetype: data[WebClientService.DATA_FIELD_BLOB_TYPE],
+            filename: data[WebClientService.DATA_FIELD_BLOB_NAME],
+        };
+        if (blobInfo.buffer === undefined || blobInfo.mimetype === undefined || blobInfo.filename === undefined) {
             this.$log.warn('Invalid blob response, data field missing');
             return this.promiseRequestError('invalidResponse');
         }
 
-        this.blobCache.set(msgId + receiverType, buffer);
+        this.blobCache.set(msgId + receiverType, blobInfo);
 
         // Download to browser
         return {
             success: true,
-            data: buffer,
+            data: blobInfo,
         };
-
     }
 
     private _receiveUpdateMessages(message: threema.WireMessage): void {

+ 7 - 1
src/threema.d.ts

@@ -117,6 +117,12 @@ declare namespace threema {
         address?: string;
     }
 
+    interface BlobInfo {
+        buffer: ArrayBuffer;
+        mimetype: string;
+        filename: string;
+    }
+
     /**
      * All possible receiver types.
      */
@@ -285,7 +291,7 @@ declare namespace threema {
         position: number;
         messageCount: number;
         unreadCount: number;
-        latestMessage: Message;
+        latestMessage: Message | null;
         receiver?: Receiver;
         avatar?: ArrayBuffer;
         isMuted?: boolean;

+ 0 - 61
tests/service/message.js

@@ -255,65 +255,4 @@ describe('MessageService', function() {
         });
     });
 
-    describe('getFileName', () => {
-        it('image', () => {
-            expect(messageService.getFileName({type: 'image', id: '123'}))
-                .toEqual('image-123.jpg');
-        });
-
-        it('audio', () => {
-            expect(messageService.getFileName({type: 'audio', id: '123'}))
-                .toEqual('audio-123.mp4');
-        });
-
-        it('video', () => {
-            expect(messageService.getFileName({type: 'video', id: '123'}))
-                .toEqual('video-123.mpg');
-        });
-
-        it('file', () => {
-            expect(messageService.getFileName({type: 'file', id: '123', file: {
-                type: 'xyz',
-                name: 'the-quick-fox.xyz'
-            }})).toEqual('the-quick-fox.xyz');
-        });
-
-        it('file without file object', () => {
-            expect(messageService.getFileName({type: 'file', id: '123'}))
-                .toEqual('file-123');
-        });
-
-        it('text', () => {
-            expect(messageService.getFileName({type: 'text', id: '123'}))
-                .toEqual(null);
-        });
-
-        it('no file types', () => {
-            ['text', 'location', 'status', 'ballot'].forEach((type) => {
-                expect(messageService.getFileName({type: type, id: '123'}))
-                    .toEqual(null);
-            });
-        });
-
-        it('no type', () => {
-            expect(messageService.getFileName({id: 1}))
-                .toEqual(null);
-        });
-        it('no id', () => {
-            expect(messageService.getFileName({type: 'image'}))
-                .toEqual(null);
-        });
-
-        it('no type', () => {
-            expect(messageService.getFileName({id: 1}))
-                .toEqual(null);
-        });
-        it('no message', () => {
-            expect(messageService.getFileName(null))
-                .toEqual(null);
-            expect(messageService.getFileName())
-                .toEqual(null);
-        });
-
-    });
 });