/
CreateMTDFile.js
61 lines (56 loc) · 1.77 KB
/
CreateMTDFile.js
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
/**
* Created by tushar.mathur on 29/06/16.
*/
'use strict'
import {mux} from 'muxer'
import R from 'ramda'
import {Observable as O} from 'rx'
import {
RemoteFileSize$,
CreateMeta$,
CreateWriteBufferAtParams,
JSToBuffer$
} from './Utils'
/**
* Creates a new .mtd file that is a little larger in size than the original
* file. The file is initially empty and has all the relevant meta
* information regarding the download appended to the end.
* @function
* @param {object} options - The `options` must have `mtdPath` and `url`.
* @param {string} options.url - Download url.
* @param {string} options.path - Relative path where the file needs to be saved.
* @param {number} [options.range=3] - Number of concurrent downloads.
* @param {number} [options.metaWrite=300] - Throttles the write frequency of meta data.
* @return {external:Observable}
* A {@link https://github.com/tusharmath/muxer multiplexed stream} containing ~
* - `written$` - Bytes being saved on disk.
* - `meta$` - Meta information about the download.
* - `remoteFileSize$` - Size of the content that is to be downloaded.
* - `fdW$` - File descriptor in `w` mode.
*/
export const CreateMTDFile = R.curry(({FILE, HTTP}, options) => {
/**
* Create a new file
*/
const fd$ = FILE.open(O.just([options.mtdPath, 'w']))
/**
* Retrieve file size on remote server
*/
const size$ = RemoteFileSize$({HTTP, options})
/**
* Create initial meta data
*/
const meta$ = CreateMeta$({options, size$})
/**
* Create a new file with meta info appended at the end
*/
const written$ = FILE.write(
CreateWriteBufferAtParams({
FILE,
fd$: fd$,
buffer$: JSToBuffer$(meta$),
position$: size$
})
)
return mux({written$, meta$, remoteFileSize$: size$, fdW$: fd$})
})