import mutagen
from archive.models import Audiobook
-from archive.settings import FILES_PATH
+from archive.settings import FILES_PATH, NEW_PATH
from archive.utils import ExistingFile, sha1_file
class AudiobookForm(forms.ModelForm):
model = Audiobook
def save(self, commit=True, path=None):
+ """ Performs normal save, with given file as an source audiobook.
+
+ `path' is relative to NEW_PATH.
+ """
m = super(AudiobookForm, self).save(commit=False)
m.modified = datetime.now()
os.makedirs(FILES_PATH)
# save the file in model
+ abs_path = os.path.join(NEW_PATH, path)
m.source_file.save(
- os.path.basename(path),
- ExistingFile(path))
+ path,
+ ExistingFile(abs_path))
f = open(m.source_file.path)
m.source_sha1 = sha1_file(f)
# -*- coding: utf-8 -*-
+import os.path
from django.db import models
from jsonfield.fields import JSONField
from django.utils.translation import ugettext_lazy as _
from archive.constants import status
-from archive.settings import FILES_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
+from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
from archive.utils import OverwriteStorage
# Create your models here.
return self.name
+def source_upload_to(intance, filename):
+ return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
+
+
class Audiobook(models.Model):
- source_file = models.FileField(upload_to='archive/files', max_length=255,
+ source_file = models.FileField(upload_to=source_upload_to, max_length=255,
verbose_name=_('source file'), editable=False)
source_sha1 = models.CharField(max_length=40, editable=False)
FILES_PATH = os.path.abspath(os.path.join(settings.MEDIA_ROOT,
"archive/files"))
+if FILES_PATH.startswith(settings.MEDIA_ROOT):
+ FILES_SAVE_PATH = FILES_PATH[len(settings.MEDIA_ROOT):].lstrip('/')
+
# here the app keeps the unmanaged (archive) files
try:
'-ab', '64k',
'-ac', '1',
'-y',
+ '-acodec', 'libmp3lame',
out_path
])
@staticmethod
def encode(in_path, out_path):
# 44.1kHz 64kbps mono Ogg Vorbis
- subprocess.check_call(['oggenc',
- in_path,
- '--discard-comments',
- '--resample', '44100',
- '--downmix',
- '-b', '64',
- '-o', out_path
+ subprocess.check_call(['ffmpeg',
+ '-i', in_path,
+ '-ar', '44100',
+ '-ab', '64k',
+ '-ac', '1',
+ '-y',
+ '-acodec', 'libvorbis',
+ out_path
])
{% block content %}
+<p>Plik źródłowy: <a href='{{ audiobook.source_file.url }}'>{{ path }}</a>
+(sha1: <tt>{{ audiobook.source_sha1 }}</tt>).
+</p>
+
<h2>{% trans "Publishing" %}</h2>
{% if audiobook.mp3_status or audiobook.ogg_status %}
url(r'^$', 'django.views.generic.simple.redirect_to', {'url': 'new/'}),
url(r'^new/$', 'archive.views.list_new', name="list_new"),
- url(r'^new/([^/]+)/$', 'archive.views.file_new', name="file_new"),
- url(r'^move_to_archive/([^/]+)/$', 'archive.views.move_to_archive', name="move_to_archive"),
+ url(r'^new/(.+)/$', 'archive.views.file_new', name="file_new"),
+ url(r'^move_to_archive/(.+)/$', 'archive.views.move_to_archive', name="move_to_archive"),
url(r'^unpublished/$', 'archive.views.list_unpublished', name="list_unpublished"),
url(r'^publishing/$', 'archive.views.list_publishing', name="list_publishing"),
url(r'^cancel/(\d+)/$', 'archive.views.cancel_publishing', name="cancel_publishing"),
url(r'^unmanaged/$', 'archive.views.list_unmanaged', name="list_unmanaged"),
- url(r'^unmanaged/([^/]+)/$', 'archive.views.file_unmanaged', name="file_unmanaged"),
- url(r'^move_to_new/([^/]+)/$', 'archive.views.move_to_new', name="move_to_new"),
+ url(r'^unmanaged/(.+)/$', 'archive.views.file_unmanaged', name="file_unmanaged"),
+ url(r'^move_to_new/(.+)/$', 'archive.views.move_to_new', name="move_to_new"),
)
from hashlib import sha1
+import os
+import os.path
from django.core.files.storage import FileSystemStorage
from django.core.files.uploadedfile import UploadedFile
for piece in iter(lambda: f.read(1024*1024), ''):
sha.update(piece)
return sha.hexdigest()
+
+
+def all_files(root_path):
+ root_len = len(root_path)
+ for path, dirs, files in os.walk(root_path):
+ for fname in files:
+ yield os.path.join(path, fname)[root_len:].lstrip('/')
+
from archive import models
from archive.forms import AudiobookForm
from archive import tasks
+from archive.utils import all_files
@login_required
division = 'new'
path = settings.NEW_PATH
- objects = sorted(os.listdir(path))
+ objects = sorted(all_files(path))
return render(request, "archive/list_new.html", locals())
def file_new(request, filename):
division = 'new'
- filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
+ filepath = filename.encode('utf-8')
+ root_filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
if request.POST:
form = AudiobookForm(request.POST)
if form.is_valid():
return redirect(list_new)
try:
- tags = mutagen.File(filepath)
+ tags = mutagen.File(root_filepath)
except IOError:
raise Http404
d = {}
- for tag in tags:
- value = tags[tag]
- if isinstance(value, list):
- d[tag] = value[0]
- else:
- d[tag] = value
- if tag == 'project':
- try:
- d[tag] = models.Project.objects.get(name=d[tag]).pk
- except models.Project.DoesNotExist:
- d[tag] = None
+ if tags:
+ for tag in tags:
+ value = tags[tag]
+ if isinstance(value, list):
+ d[tag] = value[0]
+ else:
+ d[tag] = value
+ if tag == 'project':
+ try:
+ d[tag] = models.Project.objects.get(name=d[tag]).pk
+ except models.Project.DoesNotExist:
+ d[tag] = None
if not request.POST:
form = AudiobookForm(d)
filename_str = filename.encode('utf-8')
old_path = os.path.join(settings.NEW_PATH, filename_str)
- if not os.path.isdir(settings.UNMANAGED_PATH):
- os.makedirs(settings.UNMANAGED_PATH)
new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
+ new_dir = os.path.split(new_path)[0]
+ if not os.path.isdir(new_dir):
+ os.makedirs(new_dir)
if not os.path.isfile(old_path):
raise Http404
filename_str = filename.encode('utf-8')
old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
- if not os.path.isdir(settings.NEW_PATH):
- os.makedirs(settings.NEW_PATH)
new_path = os.path.join(settings.NEW_PATH, filename_str)
+ new_dir = os.path.split(new_path)[0]
+ if not os.path.isdir(new_dir):
+ os.makedirs(new_dir)
if not os.path.isfile(old_path):
raise Http404
raise Http404
division = 'published' if audiobook.published() else 'unpublished'
+ path = audiobook.source_file.path[len(settings.FILES_PATH):].lstrip('/')
# for tags update
tags = mutagen.File(audiobook.source_file.path)
+ if not tags:
+ tags = {}
form = AudiobookForm(instance=audiobook)
return render(request, "archive/file_managed.html", locals())
def list_unmanaged(request):
division = 'unmanaged'
- objects = sorted(os.listdir(settings.UNMANAGED_PATH))
+ objects = sorted(all_files(settings.UNMANAGED_PATH))
return render(request, "archive/list_unmanaged.html", locals())
division = 'unmanaged'
tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
+ if not tags:
+ tags = {}
+
err_exists = request.GET.get('exists')
return render(request, "archive/file_unmanaged.html", locals())