-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): move CreateMTDFile and DownloadFromMTDFile to their …
…own respective files
- Loading branch information
1 parent
c245a87
commit 83593e2
Showing
6 changed files
with
121 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Created by tushar.mathur on 29/06/16. | ||
*/ | ||
|
||
'use strict' | ||
import {mux} from 'muxer' | ||
import {Observable as O} from 'rx' | ||
import { | ||
RemoteFileSize$, | ||
CreateMeta$, | ||
CreateWriteBufferAtParams, | ||
JSToBuffer$ | ||
} from './Utils' | ||
|
||
export const CreateMTDFile = ({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$}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Created by tushar.mathur on 29/06/16. | ||
*/ | ||
|
||
'use strict' | ||
|
||
import {mux} from 'muxer' | ||
import {Observable as O} from 'rx' | ||
import { | ||
CreateWriteBufferAtParams, | ||
JSToBuffer$, | ||
LocalFileSize$, | ||
MetaPosition$, | ||
ReadJSON$, | ||
demuxFPH, | ||
RequestWithMeta, | ||
WriteBuffer, | ||
SetMetaOffsets, | ||
RxThrottleComplete | ||
} from './Utils' | ||
|
||
export const DownloadFromMTDFile = ({FILE, HTTP, mtdPath}) => { | ||
/** | ||
* Open file to read+append | ||
*/ | ||
const fd$ = FILE.open(O.just([mtdPath, 'r+'])) | ||
|
||
/** | ||
* Retrieve File size on disk | ||
*/ | ||
const size$ = LocalFileSize$({FILE, fd$}) | ||
|
||
/** | ||
* Retrieve Meta info | ||
*/ | ||
const metaPosition$ = MetaPosition$({size$}) | ||
const meta$ = ReadJSON$({FILE, fd$, position$: metaPosition$}) | ||
|
||
/** | ||
* Make a HTTP request for each thread | ||
*/ | ||
const {response$, buffer$} = demuxFPH( | ||
['buffer$', 'response$'], RequestWithMeta(HTTP, meta$) | ||
) | ||
|
||
/** | ||
* Create write params and save buffer+offset to disk | ||
*/ | ||
const bufferWritten$ = WriteBuffer({FILE, fd$, buffer$}) | ||
|
||
/** | ||
* Update META info | ||
*/ | ||
const nMeta$ = SetMetaOffsets({meta$, bufferWritten$}) | ||
|
||
/** | ||
* Persist META to disk | ||
*/ | ||
const metaWritten$ = FILE.write(CreateWriteBufferAtParams({ | ||
fd$, | ||
buffer$: JSToBuffer$(RxThrottleComplete(meta$.pluck('metaWrite'), nMeta$)), | ||
position$: size$ | ||
})) | ||
|
||
/** | ||
* Create sink$ | ||
*/ | ||
return mux({ | ||
metaWritten$, response$, | ||
localFileSize$: size$, | ||
fdR$: fd$, metaPosition$, | ||
meta$: O.merge(nMeta$, meta$) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters