aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/app/videos/shared/video.model.ts
blob: eec537c9e8b8e67996a8fe1c536d5ba038f97896 (plain) (blame)
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
export class Video {
  id: string;
  name: string;
  description: string;
  magnetUri: string;
  podUrl: string;
  isLocal: boolean;
  thumbnailPath: string;
  author: string;
  createdDate: Date;
  by: string;
  duration: string;

  private static createDurationString(duration: number): string {
    const minutes = Math.floor(duration / 60);
    const seconds = duration % 60;
    const minutes_padding = minutes >= 10 ? '' : '0';
    const seconds_padding = seconds >= 10 ? '' : '0';

    return minutes_padding + minutes.toString() + ':' + seconds_padding + seconds.toString();
  }

  private static createByString(author: string, podUrl: string): string {
    let [ host, port ] = podUrl.replace(/^https?:\/\//, '').split(':');

    if (port === '80' || port === '443') {
      port = '';
    } else {
      port = ':' + port;
    }

    return author + '@' + host + port;
  }

  constructor(hash: {
    id: string,
    name: string,
    description: string,
    magnetUri: string,
    podUrl: string,
    isLocal: boolean,
    thumbnailPath: string,
    author: string,
    createdDate: string,
    duration: number;
  }) {
    this.id = hash.id;
    this.name = hash.name;
    this.description = hash.description;
    this.magnetUri = hash.magnetUri;
    this.podUrl = hash.podUrl;
    this.isLocal = hash.isLocal;
    this.thumbnailPath = hash.thumbnailPath;
    this.author  = hash.author;
    this.createdDate = new Date(hash.createdDate);
    this.duration = Video.createDurationString(hash.duration);
    this.by = Video.createByString(hash.author, hash.podUrl);
  }

  isRemovableBy(user): boolean {
    return this.isLocal === true && user && this.author === user.username;
  }
}