77import amo
88from amo .decorators import set_modified_on
99from lib .video .ffmpeg import Video
10+ import waffle
1011
1112log = logging .getLogger ('z.devhub.task' )
1213
1314
1415@task
1516@set_modified_on
16- def resize_video (src , instance , * kw ):
17+ def resize_video (src , instance , ** kw ):
1718 """
1819 Given a preview object and a file somewhere: encode into the full
1920 preview size and generate a thumbnail.
@@ -29,26 +30,40 @@ def resize_video(src, instance, *kw):
2930 log .info ('Video is not valid for %s' % instance .pk )
3031 return
3132
32- # Do the video first, this can take a bit.
33- try :
34- video_file = video .get_encoded (amo .ADDON_PREVIEW_SIZES [1 ])
35- except Exception :
36- log .info ('Error encoding video for %s' % instance .pk )
37- return
33+ if waffle .switch_is_active ('video-encode' ):
34+ # Do the video encoding.
35+ try :
36+ video_file = video .get_encoded (amo .ADDON_PREVIEW_SIZES [1 ])
37+ except Exception :
38+ log .info ('Error encoding video for %s' % instance .pk )
39+ return
3840
3941 # Do the thumbnail next, this will be the signal that the
4042 # encoding has finished.
4143 try :
4244 thumbnail_file = video .get_screenshot (amo .ADDON_PREVIEW_SIZES [0 ])
4345 except Exception :
4446 # We'll have this file floating around because the video
45- # encoded successfully.
46- os .remove (video_file )
47+ # encoded successfully, or something has gone wrong in which case
48+ # we don't want the file around anyway.
49+ if waffle .switch_is_active ('video-encode' ):
50+ os .remove (video_file )
4751 log .info ('Error making thumbnail for %s' % instance .pk )
4852 return
4953
54+ for path in (instance .thumbnail_path , instance .image_path ):
55+ dirs = os .path .dirname (path )
56+ if not os .path .exists (dirs ):
57+ os .makedirs (dirs )
58+
5059 shutil .move (thumbnail_file , instance .thumbnail_path )
51- shutil .move (video_file , instance .image_path )
60+ if waffle .switch_is_active ('video-encode' ):
61+ # Move the file over, removing the temp file.
62+ shutil .move (video_file , instance .image_path )
63+ else :
64+ # We didn't re-encode the file.
65+ shutil .copyfile (src , instance .image_path )
66+
5267 instance .sizes = {'thumbnail' : amo .ADDON_PREVIEW_SIZES [0 ],
5368 'image' : amo .ADDON_PREVIEW_SIZES [1 ]}
5469 instance .save ()
0 commit comments