@@ -55,6 +55,7 @@ class File(object):
5555 is_primary = False
5656 duration = None
5757 skip_upload = False
58+ default_preset = None
5859
5960 def __init__ (
6061 self ,
@@ -91,8 +92,11 @@ def validate(self):
9192 def get_preset (self ):
9293 if self .preset :
9394 return self .preset
95+ if self .default_preset :
96+ return self .default_preset
9497 raise NotImplementedError (
95- "preset must be set if preset isn't specified when creating File object"
98+ f"preset must be set if preset and default_preset isn't specified when creating { self .__class__ .__name__ } "
99+ f"object for file { self .filename } ({ self .original_filename } )"
96100 )
97101
98102 def get_filename (self ):
@@ -195,13 +199,16 @@ def process_file(self):
195199 self .validate ()
196200 pipeline = config .FILE_PIPELINE or fallback_pipeline
197201 try :
198- metadata = pipeline .execute (self .path , context = self .context )[0 ]
202+ metadata = pipeline .execute (
203+ self .path , context = self .context , skip_cache = config .UPDATE
204+ )[0 ]
199205 metadata = metadata .to_dict ()
200206 for key in metadata :
201207 if key == "path" :
202208 # Don't overwrite the input path.
203209 continue
204210 setattr (self , key , metadata [key ])
211+ self .validate ()
205212 if not self .filename :
206213 raise InvalidFileException ("File could not be processed by pipeline" )
207214 return super ().process_file ()
@@ -219,15 +226,13 @@ class ImageDownloadFile(DownloadFile):
219226class SlideImageFile (ImageDownloadFile ):
220227 default_ext = file_formats .PNG
221228 is_primary = True
229+ default_preset = format_presets .SLIDESHOW_IMAGE
222230
223231 def __init__ (self , path , caption = "" , descriptive_text = "" , ** kwargs ):
224232 self .caption = caption
225233 self .descriptive_text = descriptive_text
226234 super (ImageDownloadFile , self ).__init__ (path , ** kwargs )
227235
228- def get_preset (self ):
229- return format_presets .SLIDESHOW_IMAGE
230-
231236
232237class ThumbnailFile (ThumbnailPresetMixin , ImageDownloadFile ):
233238 default_ext = file_formats .PNG
@@ -237,58 +242,46 @@ class AudioFile(DownloadFile):
237242 default_ext = file_formats .MP3
238243 allowed_formats = AudioCompressionHandler .EXTENSIONS
239244 is_primary = True
245+ default_preset = format_presets .AUDIO
240246
241247 def __init__ (self , path , ffmpeg_settings = None , ** kwargs ):
242248 super (AudioFile , self ).__init__ (path , ** kwargs )
243249 self .context ["audio_settings" ] = ffmpeg_settings or {}
244250
245- def get_preset (self ):
246- return self .preset or format_presets .AUDIO
247-
248251
249252class DocumentFile (DownloadFile ):
250253 default_ext = file_formats .PDF
251254 allowed_formats = {file_formats .PDF }
252255 is_primary = True
253-
254- def get_preset (self ):
255- return self .preset or format_presets .DOCUMENT
256+ default_preset = format_presets .DOCUMENT
256257
257258
258259class EPubFile (DownloadFile ):
259260 default_ext = file_formats .EPUB
260261 allowed_formats = {file_formats .EPUB }
261262 is_primary = True
262-
263- def get_preset (self ):
264- return self .preset or format_presets .EPUB
263+ default_preset = format_presets .EPUB
265264
266265
267266class BloomPubFile (DownloadFile ):
268267 default_ext = file_formats .BLOOMPUB
269268 allowed_formats = {file_formats .BLOOMPUB , file_formats .BLOOMD }
270269 is_primary = True
271-
272- def get_preset (self ):
273- return self .preset or format_presets .BLOOMPUB
270+ default_preset = format_presets .BLOOMPUB
274271
275272
276273class HTMLZipFile (DownloadFile ):
277274 default_ext = file_formats .HTML5
278275 allowed_formats = {file_formats .HTML5 }
279276 is_primary = True
280-
281- def get_preset (self ):
282- return self .preset or format_presets .HTML5_ZIP
277+ default_preset = format_presets .HTML5_ZIP
283278
284279
285280class H5PFile (DownloadFile ):
286281 default_ext = file_formats .H5P
287282 allowed_formats = {file_formats .H5P }
288283 is_primary = True
289-
290- def get_preset (self ):
291- return self .preset or format_presets .H5P_ZIP
284+ default_preset = format_presets .H5P_ZIP
292285
293286
294287class VideoFile (DownloadFile ):
@@ -300,9 +293,6 @@ def __init__(self, path, ffmpeg_settings=None, **kwargs):
300293 super (VideoFile , self ).__init__ (path , ** kwargs )
301294 self .context ["video_settings" ] = ffmpeg_settings or {}
302295
303- def get_preset (self ):
304- return self .preset
305-
306296
307297class WebVideoFile (DownloadFile ):
308298 is_primary = True
@@ -323,9 +313,6 @@ def __init__(
323313 self .context ["max_height" ] = maxheight
324314 self .context ["high_resolution" ] = high_resolution
325315
326- def get_preset (self ):
327- return self .preset
328-
329316
330317class YouTubeVideoFile (WebVideoFile ):
331318 def __init__ (self , youtube_id , ** kwargs ):
@@ -335,6 +322,7 @@ def __init__(self, youtube_id, **kwargs):
335322
336323
337324class YouTubeSubtitleFile (File ):
325+ default_preset = format_presets .VIDEO_SUBTITLE
338326 """
339327 Helper class for downloading youtube subtitles.
340328 Args:
@@ -360,13 +348,11 @@ def __init__(self, youtube_id, language=None, **kwargs):
360348 }
361349 assert self .language , "Subtitles must have a language"
362350
363- def get_preset (self ):
364- return self .preset or format_presets .VIDEO_SUBTITLE
365-
366351
367352class SubtitleFile (DownloadFile ):
368353 default_ext = file_formats .VTT
369354 allowed_formats = SubtitleConversionHandler .EXTENSIONS
355+ default_preset = format_presets .VIDEO_SUBTITLE
370356
371357 def __init__ (self , path , ** kwargs ):
372358 """
@@ -384,9 +370,6 @@ def __init__(self, path, **kwargs):
384370 "default_ext" : self .subtitlesformat ,
385371 }
386372
387- def get_preset (self ):
388- return self .preset or format_presets .VIDEO_SUBTITLE
389-
390373
391374class Base64ImageFile (ThumbnailPresetMixin , DownloadFile ):
392375 default_ext = file_formats .PNG
@@ -396,23 +379,22 @@ def __init__(self, encoding, **kwargs):
396379
397380
398381class _ExerciseBase64ImageFile (Base64ImageFile ):
399- def get_preset (self ):
400- return self .preset or format_presets .EXERCISE_IMAGE
382+ default_preset = format_presets .EXERCISE_IMAGE
401383
402384 def get_replacement_str (self ):
403385 return self .get_filename () or self .path
404386
405387
406388class _ExerciseImageFile (ImageDownloadFile ):
389+ default_preset = format_presets .EXERCISE_IMAGE
390+
407391 def get_replacement_str (self ):
408392 return self .get_filename () or self .path
409393
410- def get_preset (self ):
411- return self .preset or format_presets .EXERCISE_IMAGE
412-
413394
414395class _ExerciseGraphieFile (File ):
415396 default_ext = file_formats .GRAPHIE
397+ default_preset = format_presets .EXERCISE_GRAPHIE
416398
417399 def __init__ (self , path ):
418400 self .original_filename = path .split ("/" )[- 1 ].split ("." )[0 ]
@@ -426,9 +408,6 @@ def validate(self):
426408 except ValueError :
427409 raise ValueError ("_ExerciseGraphieFile must have a valid URL" )
428410
429- def get_preset (self ):
430- return self .preset or format_presets .EXERCISE_GRAPHIE
431-
432411 def get_replacement_str (self ):
433412 if "http" in self .path :
434413 return self .path .split ("/" )[- 1 ].split ("." )[0 ] or self .path
@@ -523,27 +502,31 @@ def extractor_fun(self, fpath_in, thumbpath_out, **kwargs):
523502
524503class ExtractedPdfThumbnailFile (ExtractedThumbnailFile ):
525504 extractor_kwargs = {"page_number" : 0 , "crop" : None }
505+ allowed_formats = DocumentFile .allowed_formats
526506
527507 def extractor_fun (self , fpath_in , thumbpath_out , ** kwargs ):
528508 create_image_from_pdf_page (fpath_in , thumbpath_out , ** kwargs )
529509
530510
531511class ExtractedEPubThumbnailFile (ExtractedThumbnailFile ):
532512 extractor_kwargs = {"crop" : None }
513+ allowed_formats = EPubFile .allowed_formats
533514
534515 def extractor_fun (self , fpath_in , thumbpath_out , ** kwargs ):
535516 create_image_from_epub (fpath_in , thumbpath_out , ** kwargs )
536517
537518
538519class ExtractedHTMLZipThumbnailFile (ExtractedThumbnailFile ):
539520 extractor_kwargs = {"crop" : "smart" }
521+ allowed_formats = HTMLZipFile .allowed_formats
540522
541523 def extractor_fun (self , fpath_in , thumbpath_out , ** kwargs ):
542524 create_image_from_zip (fpath_in , thumbpath_out , ** kwargs )
543525
544526
545527class ExtractedVideoThumbnailFile (ExtractedThumbnailFile ):
546528 extractor_kwargs = {"overwrite" : True }
529+ allowed_formats = VideoFile .allowed_formats
547530
548531 def extractor_fun (self , fpath_in , thumbpath_out , ** kwargs ):
549532 extract_thumbnail_from_video (fpath_in , thumbpath_out , ** kwargs )
0 commit comments