All files / src/dao PhotoDAO.ts

60% Statements 21/35
0% Branches 0/4
60% Functions 6/10
60% Lines 21/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 1092x 2x 2x 2x   2x   2x         1x   1x   1x   1x               1x   1x     1x   1x   1x               1x             1x       1x                 1x   1x   1x                                                                                  
import { AbstractDAO } from './AbstractDAO';
import { TwitarrError } from '../api/TwitarrError';
import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions';
import { PhotoDetails } from '../model/PhotoDetails';
 
require('buffer');
 
export class PhotoDAO extends AbstractDAO {
  /**
   * Retrieve a photo's metadata.
   */
  public async get(id: string) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
 
    return this.http
      .get('/api/v2/photo/' + id, options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return PhotoDetails.fromRest(data.photo);
      });
  }
 
  /**
   * Update the "original filename" metadata on a photo.
   */
  public async put(id: string, newFileName: string) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
 
    options.data = {
      original_filename: newFileName,
    };
    return this.http
      .put('/api/v2/photo/' + id, options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return PhotoDetails.fromRest(data.photo);
      });
  }
 
  /**
   * Delete/remove a photo..
   */
  public async remove(id: string) {
    return this.http.httpDelete('/api/v2/photo/' + id);
  }
 
  /**
   * Post a photo.
   */
  public async post(fileName: string, photoData: Buffer) {
    const options = new TwitarrHTTPOptions().withHeader('cache-control', 'no-cache');
 
    let mimeType;
    if (fileName.endsWith('.png')) {
      mimeType = 'image/png';
    } else if (fileName.endsWith('.gif')) {
      mimeType = 'image/gif';
    } else if (fileName.endsWith('.jpeg') || fileName.endsWith('.jpg')) {
      mimeType = 'image/jpeg';
    } else {
      throw new TwitarrError('Unable to determine mime-type from filename: ' + fileName);
    }
 
    return this.http
      .postFile('/api/v2/photo', fileName, mimeType, photoData, options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return PhotoDetails.fromRest(data.photo);
      });
  }
 
  /**
   * Post a profile photo.
   */
  public async profile(username: string, fileName: string, photoData: Buffer) {
    const options = new TwitarrHTTPOptions().withHeader('cache-control', 'no-cache');
 
    let mimeType;
    if (fileName.endsWith('.png')) {
      mimeType = 'image/png';
    } else if (fileName.endsWith('.gif')) {
      mimeType = 'image/gif';
    } else if (fileName.endsWith('.jpeg') || fileName.endsWith('.jpg')) {
      mimeType = 'image/jpeg';
    } else {
      throw new TwitarrError('Unable to determine mime-type from filename: ' + fileName);
    }
 
    return this.http
      .postFile('/api/v2/user/photo', fileName, mimeType, photoData, options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return PhotoDetails.fromRest(data.photo);
      });
  }
 
  /**
   * Remove the user's profile photo.
   */
  public async removeProfile() {
    return this.http
      .httpDelete('/api/v2/user/photo')
      .then(result => this.handleErrors(result))
      .then(data => {
        return data.md5_hash;
      });
  }
}