-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutputfiles.c
executable file
·305 lines (244 loc) · 6.2 KB
/
outputfiles.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "outputfiles.h"
ListNode *OutputFiles;
//---------------- Functions to do with files and file names ----------
//Strip out characters that make it difficult to specify a file on the
//command line, such as '&' or ';' '>' that mean something to the shell
char *MakeFilesystemSafeName(char *Buffer, char *Path)
{
char *Str=NULL;
char BadChars[]="'\"()/?*&<>; ";
char *ptr;
Str=CopyStr(Buffer,"");
ptr=Path;
while (ptr && (*ptr != '\0'))
{
if (strchr(BadChars,*ptr)) Str=AddCharToStr(Str,'_');
else Str=AddCharToStr(Str,*ptr);
ptr++;
}
return(Str);
}
//guess the extension of a file, some sites don't have extensions
//on their filenames
char *GuessExtn(const char *ContentType, const char *Format, const char *ID)
{
static char *Extn=NULL;
char *ptr;
Extn=CopyStr(Extn,".flv");
if (StrValid(ID))
{
ptr=strchr(ID,'.');
if (StrValid(ptr)) Extn=CopyStr(Extn,ptr);
}
if (StrValid(Format)) Extn=MCopyStr(Extn,".",Format,NULL);
if (StrValid(ContentType))
{
ptr=NULL;
if (strcasecmp(ContentType,"audio/mp3")==0) ptr=".mp3";
else if (strcasecmp(ContentType,"audio/mpeg")==0) ptr=".mp3";
else if (strcasecmp(ContentType,"audio/ogg")==0) ptr=".ogg";
else if (strcasecmp(ContentType,"audio/aac")==0) ptr=".aac";
else if (strcasecmp(ContentType,"video/x-flv")==0) ptr=".flv";
else if (strcasecmp(ContentType,"video/flv")==0) ptr=".flv";
else if (strcasecmp(ContentType,"video/mp4")==0) ptr=".mp4";
else if (strcasecmp(ContentType,"video/3gpp")==0) ptr=".3gp";
else if (strcasecmp(ContentType,"audio/webm")==0) ptr=".webm";
else if (strcasecmp(ContentType,"video/webm")==0) ptr=".webm";
if (StrValid(ptr)) Extn=CopyStr(Extn,ptr);
}
return(Extn);
}
//URL is used to provide a unique MD5 so that two downloads from
//different sites with the same title don't overwrite each other
char *GetSaveFilePath(char *RetStr, const char *Title, const char *URL)
{
char *ptr=NULL;
char *Tempstr=NULL, *MD5=NULL;
if (StrValid(Title)) ptr=Title;
else
{
Tempstr=CopyStr(Tempstr, URL);
//Assume the filename is the document part of the url
ptr=strrchr(Tempstr,'?');
if (ptr) *ptr='\0';
ptr=strrchr(Tempstr,'/');
if (ptr) ptr++;
else ptr=Tempstr;
}
RetStr=MakeFilesystemSafeName(RetStr, ptr);
ptr=strrchr(RetStr,'.');
//some titles might have '.' in them, but not have an extension
//only assume it's an extension if it's under six chars long
if (ptr && (StrLen(ptr) < 6)) *ptr='\0';
if (! (Flags & FLAG_RESUME))
{
HashBytes(&MD5,"md5",Tempstr,StrLen(Tempstr),ENCODE_HEX);
Tempstr=MCatStr(Tempstr,"-",MD5,NULL);
}
//Some stupid sites start pagenames with a space, and we don't want this in the filename
StripLeadingWhitespace(RetStr);
Destroy(Tempstr);
Destroy(MD5);
return(RetStr);
}
//Open the file we are going to save the move to. Check in the user
//filename preference stored in the global variable 'SaveFilePath'
//If that's set to '-' then open stdout and write to that, if it's
//set to anything else, then use that as the filename. Otherwise
//build the filename from available info.
STREAM *OpenSaveFile(const char *Path, double *FileSize, int ResumeDownload)
{
STREAM *S=NULL;
struct stat FStat;
*FileSize=0;
if (strcmp(Path,"-")==0)
{
S=STREAMFromFD(1);
STREAMSetTimeout(S,0);
}
else
{
S=STREAMFileOpen(Path, SF_CREAT | SF_RDWR);
if (S)
{
if (! STREAMLock(S,LOCK_EX|LOCK_NB))
{
if (! (Flags & FLAG_QUIET)) fprintf(stderr,"Already downloading this item!\n");
fflush(NULL);
exit(0);
}
//Filesize can be used to resume a part download
if (ResumeDownload)
{
fstat(S->in_fd,&FStat);
*FileSize=(double) FStat.st_size;
STREAMSeek(S,0,SEEK_END);
}
}
}
return(S);
}
STREAM *OpenCacheFile(char *Title, char *URL)
{
char *Tempstr=NULL;
STREAM *S=NULL;
glob_t Glob;
Tempstr=GetSaveFilePath(Tempstr, Title, URL);
Tempstr=CatStr(Tempstr,".*");
glob(Tempstr,0,0,&Glob);
if (Glob.gl_pathc > 0)
{
S=STREAMFileOpen(Glob.gl_pathv[0],SF_RDONLY);
}
globfree(&Glob);
Destroy(Tempstr);
return(S);
}
//some apps expect to be able to read data in multiples of 1024, so if we are writing
//to standard out (probably piped into such an app) we work in blocks
#define BLOCKSIZE 4096
void OpenOutputFiles(const char *Title, const char *URL, double *FileSize)
{
char *Tempstr=NULL;
ListNode *Curr;
int val=0, Resume=FALSE;
STREAM *S=NULL;
if (Flags & FLAG_NOOUT) return;
if ((Flags & FLAG_RESUME) && (ListSize(OutputFiles)==1)) Resume=TRUE;
Curr=ListGetNext(OutputFiles);
while (Curr)
{
if (! Curr->Item)
{
if (! StrValid(Curr->Tag))
{
Tempstr=GetSaveFilePath(Tempstr, Title, URL);
S=OpenSaveFile(Tempstr, FileSize, Resume);
Curr->Item=S;
}
else if (strcmp(Curr->Tag,"-")==0)
{
S=STREAMFromFD(1);
STREAMResizeBuffer(S, BLOCKSIZE * 8);
STREAMSetFlushType(S, FLUSH_BLOCK, BLOCKSIZE * 4, BLOCKSIZE);
Curr->Item=S;
}
else Curr->Item=OpenSaveFile(Curr->Tag, FileSize, Resume);
}
Curr=ListGetNext(Curr);
}
Curr=ListGetNext(OutputFiles);
while (Curr)
{
if (Curr->Item) S=Curr->Item;
Curr=ListGetNext(Curr);
}
if (! S)
{
printf("ERROR: FAILED TO OPEN ANY OUTPUT FILES\n");
exit(1);
}
Destroy(Tempstr);
}
void WriteOutputFiles(const char *Data, int Len)
{
ListNode *Curr;
Curr=ListGetNext(OutputFiles);
while (Curr)
{
if (Curr->Item) STREAMWriteBytes((STREAM *) Curr->Item,Data,Len);
Curr=ListGetNext(Curr);
}
}
void CloseOutputFiles(char *Extn)
{
ListNode *Curr, *Next;
char *Tempstr=NULL;
STREAM *S;
Curr=ListGetNext(OutputFiles);
while (Curr)
{
Next=ListGetNext(Curr);
if ((! Curr->Tag) || (strcmp(Curr->Tag,"-") !=0))
{
S=(STREAM *) Curr->Item;
if (S)
{
if (! StrValid(Curr->Tag))
{
Tempstr=MCopyStr(Tempstr,S->Path,Extn,NULL);
rename(S->Path,Tempstr);
}
STREAMClose(S);
}
ListDeleteNode(Curr);
}
Curr=Next;
}
Destroy(Tempstr);
}
void AddOutputStream(const char *Path, STREAM *S)
{
if (! OutputFiles) OutputFiles=ListCreate();
ListAddNamedItem(OutputFiles,Path,S);
}
void AddOutputFile(const char *Path, int SingleOutput)
{
//if 'SingleOutput' is set then overwrite any existing outputs, this should be the
//only one
if (SingleOutput) ListClear(OutputFiles,NULL);
AddOutputStream(Path, NULL);
}
char *OutputFilesGetFilePath()
{
ListNode *Curr;
STREAM *S;
Curr=ListGetNext(OutputFiles);
while (Curr)
{
S=(STREAM *) Curr->Item;
if (S && (strcmp(Curr->Tag,"-") !=0)) return(S->Path);
Curr=ListGetNext(Curr);
}
return("");
}