Do not send deleted files
This commit is contained in:
parent
f74f059f13
commit
125df3af66
2 changed files with 23 additions and 9 deletions
|
@ -10,8 +10,13 @@ class Config:
|
|||
SECRET_KEY = os.environ.get('SECRET_KEY') or \
|
||||
''.join(random_choices(string.ascii_letters, k=20))
|
||||
DATA_DIR = os.environ.get('DATA_DIR') or '/var/tmp/prolefeeder-test-data'
|
||||
TMP_DIR = os.environ.get('TMP_DIR') or '/var/tmp'
|
||||
OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out'
|
||||
MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
|
||||
KBITS = os.environ.get('KBITS') or 128
|
||||
|
||||
DEBUG = False
|
||||
|
||||
if not os.path.exists(DATA_DIR):
|
||||
raise "DATA_DIR does not exist"
|
||||
if not os.path.exists(OUT_DIR):
|
||||
raise "OUT_DIR does not exist"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from flask import (Flask, render_template, flash, redirect,
|
||||
url_for, request, send_file, abort)
|
||||
url_for, request, send_from_directory, abort)
|
||||
from flask_bootstrap import Bootstrap
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import DateTimeField, IntegerField, SubmitField
|
||||
|
@ -11,6 +11,7 @@ import os
|
|||
import random
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from config import Config
|
||||
|
||||
|
@ -56,6 +57,16 @@ def download():
|
|||
return render_template('download.html', form=form)
|
||||
|
||||
|
||||
def clean_out_dir():
|
||||
"""Clean up OUT_DIR"""
|
||||
for fn in os.listdir(app.config['OUT_DIR']):
|
||||
if not fn.startswith('prolefeeder'):
|
||||
continue
|
||||
creation_time = os.path.getctime(os.path.join(app.config['OUT_DIR'], fn))
|
||||
if time.time() - creation_time >= 60:
|
||||
os.unlink(os.path.join(app.config['OUT_DIR'], fn))
|
||||
|
||||
|
||||
@app.route('/download_file/<filename>')
|
||||
def download_file(filename):
|
||||
"""Download an output file"""
|
||||
|
@ -64,12 +75,10 @@ def download_file(filename):
|
|||
abort(404)
|
||||
attachment_filename = attachment_filenames.pop(filename)
|
||||
|
||||
fh = open(os.path.join(app.config['TMP_DIR'], filename), 'rb')
|
||||
os.remove(os.path.join(app.config['TMP_DIR'], filename))
|
||||
clean_out_dir()
|
||||
|
||||
# XXX How to close fh?
|
||||
return send_file(fh, as_attachment=True,
|
||||
attachment_filename=attachment_filename)
|
||||
return send_from_directory(app.config['OUT_DIR'], filename,
|
||||
as_attachment=True, attachment_filename=attachment_filename)
|
||||
|
||||
|
||||
def prepare_download(form):
|
||||
|
@ -110,7 +119,7 @@ def prepare_download(form):
|
|||
ss = (form.start_time.data - sources[0]['start_time']).total_seconds()
|
||||
|
||||
# Let ffmpeg do the rest of the job
|
||||
_, output_filename = mkstemp(suffix='.mp3', dir=app.config['TMP_DIR'])
|
||||
_, output_filename = mkstemp(prefix='prolefeeder', suffix='.mp3', dir=app.config['OUT_DIR'])
|
||||
output_filename = os.path.basename(output_filename)
|
||||
attachment_filename = '{}_{}.mp3'.format(form.start_time.data, form.length.data)
|
||||
|
||||
|
@ -121,7 +130,7 @@ def prepare_download(form):
|
|||
[os.path.join(app.config['DATA_DIR'], s['fn']) for s in sources]),
|
||||
'-codec', 'copy',
|
||||
'-t', str(form.length.data * 60),
|
||||
os.path.join(app.config['TMP_DIR'], output_filename)
|
||||
os.path.join(app.config['OUT_DIR'], output_filename)
|
||||
]
|
||||
app.logger.debug(' '.join(c))
|
||||
subprocess.call(c)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue