2017-01-29 20:06:08 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-06-05 15:41:47 +00:00
|
|
|
import os
|
2016-10-30 10:44:02 +00:00
|
|
|
from tempfile import gettempdir
|
2016-06-05 15:41:47 +00:00
|
|
|
import hashlib
|
|
|
|
from collections import namedtuple
|
|
|
|
import book_formats
|
|
|
|
|
2017-03-04 07:37:30 +00:00
|
|
|
BookMeta = namedtuple('BookMeta', 'file_path, extension, title, author, cover, description, tags, series, series_id, languages')
|
2016-06-05 15:41:47 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
:rtype: BookMeta
|
|
|
|
"""
|
2016-12-23 08:53:39 +00:00
|
|
|
|
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
def upload(uploadfile):
|
2016-10-30 10:44:02 +00:00
|
|
|
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
|
|
|
|
|
2016-06-05 15:41:47 +00:00
|
|
|
if not os.path.isdir(tmp_dir):
|
|
|
|
os.mkdir(tmp_dir)
|
|
|
|
|
2017-11-30 15:49:46 +00:00
|
|
|
filename = uploadfile.filename
|
2016-06-05 15:41:47 +00:00
|
|
|
filename_root, file_extension = os.path.splitext(filename)
|
|
|
|
md5 = hashlib.md5()
|
2016-10-30 10:44:02 +00:00
|
|
|
md5.update(filename.encode('utf-8'))
|
2016-06-05 15:41:47 +00:00
|
|
|
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
|
2017-11-30 15:49:46 +00:00
|
|
|
uploadfile.save(tmp_file_path)
|
2016-06-05 15:41:47 +00:00
|
|
|
meta = book_formats.process(tmp_file_path, filename_root, file_extension)
|
|
|
|
return meta
|