Completed
Push — master ( 61d444...b3f358 )
by Jan
16s queued 14s
created

src/frames/frame-comr.ts   A

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 102
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 83
mnd 7
bc 7
fnc 0
dl 0
loc 102
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import fs = require('fs')
2
import { FrameBuilder } from "../FrameBuilder"
3
import { FrameReader } from "../FrameReader"
4
import * as ID3Util from "../ID3Util"
5
import { isString } from '../util'
6
import type { Data } from "./type"
7
8
export const COMR = {
9
    create: (data: Data) => {
10
        if(!(data instanceof Array)) {
11
            data = [data]
12
        }
13
14
        return Buffer.concat(data.map((comr: Data) => {
15
            const prices = comr.prices || {}
16
            const builder = new FrameBuilder("COMR")
17
18
            // Text encoding
19
            builder.appendNumber(0x01, 1)
20
            // Price string
21
            const priceString = Object.entries(prices).map((price: Data) => {
22
                return price[0].substring(0, 3) + price[1].toString()
23
            }).join('/')
24
            builder.appendNullTerminatedValue(priceString, 0x00)
25
            // Valid until
26
            builder.appendValue(
27
                comr.validUntil.year.toString().padStart(4, '0').substring(0, 4) +
28
                comr.validUntil.month.toString().padStart(2, '0').substring(0, 2) +
29
                comr.validUntil.day.toString().padStart(2, '0').substring(0, 2),
30
                8, 0x00
31
            )
32
            // Contact URL
33
            builder.appendNullTerminatedValue(comr.contactUrl, 0x00)
34
            // Received as
35
            builder.appendNumber(comr.receivedAs, 1)
36
            // Name of seller
37
            builder.appendNullTerminatedValue(comr.nameOfSeller, 0x01)
38
            // Description
39
            builder.appendNullTerminatedValue(comr.description, 0x01)
40
            // Seller logo
41
            if(comr.sellerLogo) {
42
                const pictureFilenameOrBuffer = comr.sellerLogo.picture
43
                const picture = isString(pictureFilenameOrBuffer)
44
                    ? fs.readFileSync(comr.sellerLogo.picture)
45
                    : pictureFilenameOrBuffer
46
47
                let mimeType = comr.sellerLogo.mimeType || ID3Util.getPictureMimeTypeFromBuffer(picture)
48
49
                // Only image/png and image/jpeg allowed
50
                if (mimeType !== 'image/png' && 'image/jpeg') {
51
                    mimeType = 'image/'
52
                }
53
54
                builder.appendNullTerminatedValue(mimeType || '', 0x00)
55
                builder.appendValue(picture)
56
            }
57
            return builder.getBuffer()
58
        }))
59
    },
60
    read: (buffer: Buffer) => {
61
        const reader = new FrameReader(buffer, 0)
62
63
        const tag: Data = {}
64
65
        // Price string
66
        const priceStrings = reader.consumeNullTerminatedValue('string', 0x00)
67
            .split('/')
68
            .filter((price) => price.length > 3)
69
        tag.prices = {}
70
        for(const price of priceStrings) {
71
            tag.prices[price.substring(0, 3)] = price.substring(3)
72
        }
73
        // Valid until
74
        const validUntilString = reader.consumeStaticValue('string', 8, 0x00)
75
        tag.validUntil = { year: 0, month: 0, day: 0 }
76
        if(/^\d+$/.test(validUntilString)) {
77
            tag.validUntil.year = parseInt(validUntilString.substring(0, 4))
78
            tag.validUntil.month = parseInt(validUntilString.substring(4, 6))
79
            tag.validUntil.day = parseInt(validUntilString.substring(6))
80
        }
81
        // Contact URL
82
        tag.contactUrl = reader.consumeNullTerminatedValue('string', 0x00)
83
        // Received as
84
        tag.receivedAs = reader.consumeStaticValue('number', 1)
85
        // Name of seller
86
        tag.nameOfSeller = reader.consumeNullTerminatedValue('string')
87
        // Description
88
        tag.description = reader.consumeNullTerminatedValue('string')
89
        // Seller logo
90
        const mimeType = reader.consumeNullTerminatedValue('string', 0x00)
91
        const picture = reader.consumeStaticValue('buffer')
92
        if(picture && picture.length > 0) {
93
            tag.sellerLogo = {
94
                mimeType,
95
                picture
96
            }
97
        }
98
99
        return tag
100
    }
101
}
102