1+ // nodejs fs
2+ var fs = require ( 'fs' ) ;
3+
4+ // nodejs http
5+ var http = require ( 'http' ) ;
6+
7+ var DirectoryManager = require ( './directoryManager' ) . directoryManager ;
8+
9+ var FileManager = require ( './fileManager' ) . fileManager ;
10+
11+
12+ /**
13+ *
14+ * downloader
15+ *
16+ * @returns {downloaderConstructor }
17+ */
18+ var downloader = function downloaderConstructor ( ) {
19+
20+ } ;
21+
22+ /**
23+ *
24+ * fetches a file from a remote server and writes it into a folder on disc
25+ *
26+ * @param {type } options
27+ * @param {type } callback
28+ * @returns {undefined }
29+ */
30+ downloader . prototype . writeToDisc = function ( options , callback ) {
31+
32+ if ( options === undefined ) {
33+
34+ options = { } ;
35+
36+ }
37+
38+ if ( options . serverDirecotry === undefined ) {
39+
40+ options . serverDirecotry = './downloads' ;
41+
42+ }
43+
44+ var directoryManager = new DirectoryManager ( ) ;
45+
46+ var that = this ;
47+
48+ // check if the temporary tracks directory already exists
49+ directoryManager . exists ( options . serverDirecotry , function directoryExistsCallback ( error , exists ) {
50+
51+ // if there was no error checking if the directory exists
52+ if ( ! error ) {
53+
54+ // if the directory does not exist
55+ if ( ! exists ) {
56+
57+ // create a new directory
58+ directoryManager . create ( options . serverDirecotry , createDirectoryCallback = function ( error ) {
59+
60+ // if there was no error creating the new directory
61+ if ( ! error ) {
62+
63+ // download the the track and store it on disc
64+ that . downloadIfNotExists ( options , callback ) ;
65+
66+ } else {
67+
68+ callback ( error ) ;
69+
70+ }
71+
72+ } ) ;
73+
74+ } else {
75+
76+ // download the the track and store it on disc
77+ that . downloadIfNotExists ( options , callback ) ;
78+
79+ }
80+
81+ } else {
82+
83+ callback ( error ) ;
84+
85+ }
86+
87+ } ) ;
88+
89+ } ;
90+
91+ /**
92+ *
93+ * donwloads a track if does not already exist on disc
94+ *
95+ * @param {type } options
96+ * @param {type } callback
97+ * @returns {undefined }
98+ */
99+ downloader . prototype . downloadIfNotExists = function downloadIfNotExists ( options , callback ) {
100+
101+ var fileManager = new FileManager ( ) ;
102+
103+ var filePath = options . serverDirecotry + '/' + options . fileName ;
104+
105+ var that = this ;
106+
107+ // check if the file already exists
108+ fileManager . exists ( filePath , function fileExistsCallback ( error , exists ) {
109+
110+ // if there was no error checking if the file exists
111+ if ( ! error ) {
112+
113+ if ( ! exists ) {
114+
115+ // download the file and store it in the temporary directory
116+ that . downloadFile ( options , callback ) ;
117+
118+ } else {
119+
120+ callback ( false , filePath ) ;
121+
122+ }
123+
124+ } else {
125+
126+ callback ( error ) ;
127+
128+ }
129+
130+ } ) ;
131+
132+ } ;
133+
134+ /**
135+ *
136+ * download a file
137+ *
138+ * @param {type } downloadOptions
139+ * @param {type } callback
140+ * @returns {undefined }
141+ */
142+ downloader . prototype . downloadFile = function downloadFileFunction ( downloadOptions , callback ) {
143+
144+ console . log ( 'downloadFile: ' + downloadOptions . path ) ;
145+
146+ if ( downloadOptions === undefined ) {
147+
148+ callback ( 'downloadOptions is undefined' ) ;
149+
150+ }
151+
152+ if ( downloadOptions . method === undefined ) {
153+
154+ downloadOptions . method = 'GET' ;
155+
156+ }
157+
158+ if ( downloadOptions . remotePort === undefined ) {
159+
160+ downloadOptions . port = 80 ;
161+
162+ }
163+
164+ if ( downloadOptions . remoteHost === undefined ) {
165+
166+ callback ( 'download host is undefined' ) ;
167+
168+ }
169+
170+ if ( downloadOptions . remotePath === undefined ) {
171+
172+ callback ( 'download path is undefined' ) ;
173+
174+ }
175+
176+ var writeStream = fs . createWriteStream ( downloadOptions . filePath + downloadOptions . fileName ) ;
177+
178+ // open a new write stream
179+ writeStream . on ( 'open' , function ( ) {
180+
181+ var requestOptions = {
182+ hostname : downloadOptions . remoteHost ,
183+ port : downloadOptions . remotePort ,
184+ path : downloadOptions . remotePath ,
185+ method : downloadOptions . method
186+ } ;
187+
188+ // request the file from remote server
189+ var httpRequest = http . request ( requestOptions , function ( httpResponse ) {
190+
191+ console . log ( 'writeTrackToDisc httpRequest STATUS: ' + httpResponse . statusCode ) ;
192+ console . log ( 'writeTrackToDisc httpRequest HEADERS: ' + JSON . stringify ( httpResponse . headers ) ) ;
193+
194+ // on successful request
195+ httpResponse . on ( 'data' , function ( chunk ) {
196+
197+ // write the file
198+ writeStream . write ( chunk ) ;
199+
200+ } ) ;
201+
202+ // the connection got closed
203+ httpResponse . on ( 'end' , function ( ) {
204+
205+ console . log ( 'remote file: ' + downloadOptions . fileName + ', got downloaded into: ' + downloadOptions . serverDirectory ) ;
206+
207+ // close the write stream
208+ writeStream . end ( ) ;
209+
210+ callback ( false , downloadOptions . serverDirectory + downloadOptions . fileName ) ;
211+
212+ } ) ;
213+
214+ } ) ;
215+
216+ // the request to the remote server failed
217+ httpRequest . on ( 'error' , function ( error ) {
218+
219+ console . log ( 'writeToDisc, http request error: ' + error . message ) ;
220+
221+ writeStream . end ( ) ;
222+
223+ callback ( error ) ;
224+
225+ } ) ;
226+
227+ httpRequest . end ( ) ;
228+
229+ } ) ;
230+
231+ // writing the file failed
232+ writeStream . on ( 'error' , function ( error ) {
233+
234+ console . log ( 'writeToDisc writeStream, error: ' + error ) ;
235+
236+ // close the stream
237+ writeStream . end ( ) ;
238+
239+ callback ( error ) ;
240+
241+ } ) ;
242+
243+ } ;
244+
245+ module . exports . downloader = downloader ;
0 commit comments