def _store_source(track_id, ftype, session_key, source=False): tr = Track.objects.get(pk=track_id) print 'starting to store %s for %s from %s' % (ftype, track_id, tr.temp_path) if filetype(tr.temp_path) != ftype: raise ValueError('%s is not of type %s' % (tr.temp_path, ftype)) s3.move_local_file_into_s3_dir(tr.temp_path, tr.s3_url(ftype), make_public=False, make_protected=True, unlink_source=False) TrackFile.from_file(tr, tr.temp_path, session_key, source=source) print '%s stored for %s' % (ftype, track_id)
def store_and_transcode(track_id, session_key): tr = Track.objects.get(pk=track_id) ftype = filetype(tr.temp_path) transcoders = [store_ogg] if ftype == 'mp3': store_source = store_mp3 elif ftype == 'm4a': store_source = store_m4a else: raise ValueError('file type not supported: %r' % ftype) args = [tr.pk, session_key] pipeline = TaskTree() pipeline.push(store_source, args=args, kwargs=dict(source=True)) for trans in transcoders: pipeline.push(trans, args=args) pipeline.push(unlink_source, args=args) pipeline.apply_async()
def create_audio_file(source=None, make_mp3=False, make_ogg=False, session=None, **af_params): if not source: source = os.path.join(os.path.dirname(__file__), 'resources', 'sample.mp3') if not os.path.exists(settings.UPLOAD_TEMP_DIR): os.makedirs(settings.UPLOAD_TEMP_DIR) ftype = filetype(source) tmp = os.path.join(settings.UPLOAD_TEMP_DIR, '__test__.%s' % ftype) if os.path.exists(tmp): os.unlink(tmp) shutil.copyfile(source, tmp) try: em = VerifiedEmail.objects.get(email='*****@*****.**') except VerifiedEmail.DoesNotExist: em = VerifiedEmail.objects.create(email='*****@*****.**') params = dict(temp_path=tmp, email=em, artist='Gescom', album='Minidisc', track='Horse') params.update(af_params) tr = Track.objects.create(**params) # These are convenience functions to copy the source into stub locations. # Don't rely on them too much. if make_mp3: TrackFile.objects.create(track=tr, type='mp3', sha1='noop', byte_size=1, session=session, s3_url='s3:file.mp3') if make_ogg: TrackFile.objects.create(track=tr, type='ogg', sha1='noop', byte_size=1, session=session, s3_url='s3:file.ogg') return tr
def from_file(cls, track, filename, session_key, source=False): """Creates a track file from a filename. if source is True it means that this file was the original one uploaded for the track. """ hash = hashlib.sha1() with open(filename, 'rb') as fp: while 1: chunk = fp.read(1024 * 100) if not chunk: break hash.update(chunk) sha1 = hash.hexdigest() type = filetype(filename) tf = cls.objects.create(track=track, sha1=sha1, s3_url=track.s3_url(type), type=type, session_id=session_key, byte_size=os.path.getsize(filename)) if source: Track.objects.filter(pk=track.pk).update(source_track_file=tf) return tf