248 lines
7.8 KiB
Python
248 lines
7.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
|
from datetime import datetime
|
|
import os
|
|
import shutil
|
|
import time
|
|
|
|
import ox
|
|
|
|
from changelog import Changelog
|
|
from item.models import File
|
|
from user.models import List
|
|
from utils import remove_empty_folders
|
|
from websocket import trigger_event
|
|
import db
|
|
import media
|
|
import settings
|
|
import state
|
|
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
extensions = ['epub', 'pdf', 'txt', 'cbr', 'cbz']
|
|
|
|
def remove_missing():
|
|
dirty = False
|
|
with db.session():
|
|
prefs = settings.preferences
|
|
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books' + os.sep)
|
|
if os.path.exists(prefix):
|
|
for f in File.query:
|
|
if not state.tasks.connected:
|
|
return
|
|
if f.item:
|
|
path = f.item.get_path()
|
|
if not os.path.exists(path):
|
|
dirty = True
|
|
f.item.remove_file()
|
|
else:
|
|
state.db.session.delete(f)
|
|
dirty = True
|
|
if dirty:
|
|
state.db.session.commit()
|
|
for f in File.query:
|
|
if not state.tasks.connected:
|
|
return
|
|
f.move()
|
|
remove_empty_folders(prefix, True)
|
|
|
|
def add_file(id, f, prefix, from_=None):
|
|
user = state.user()
|
|
path = f[len(prefix):]
|
|
logger.debug('%s extract metadata %s', id, path)
|
|
data = media.metadata(f, from_)
|
|
logger.debug('%s create file %s', id, path)
|
|
file = File.get_or_create(id, data, path)
|
|
item = file.item
|
|
item.add_user(user)
|
|
item.added = datetime.utcnow()
|
|
logger.debug('%s load metadata %s', id, path)
|
|
item.load_metadata()
|
|
Changelog.record(user, 'additem', item.id, file.info)
|
|
Changelog.record(user, 'edititem', item.id, item.meta)
|
|
logger.debug('%s extract icons %s', id, path)
|
|
item.update_icons()
|
|
item.modified = datetime.utcnow()
|
|
logger.debug('%s save item', id)
|
|
item.update()
|
|
logger.debug('%s added', id)
|
|
return file
|
|
|
|
def run_scan():
|
|
remove_missing()
|
|
prefs = settings.preferences
|
|
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books' + os.sep)
|
|
if not prefix[-1] == os.sep:
|
|
prefix += os.sep
|
|
assert isinstance(prefix, str)
|
|
books = []
|
|
for root, folders, files in os.walk(prefix):
|
|
for f in files:
|
|
if not state.tasks.connected:
|
|
return
|
|
#if f.startswith('._') or f == '.DS_Store':
|
|
if f.startswith('.'):
|
|
continue
|
|
f = os.path.join(root, f)
|
|
ext = f.split('.')[-1]
|
|
if ext == 'kepub':
|
|
ext = 'epub'
|
|
if ext in extensions:
|
|
books.append(f)
|
|
|
|
position = 0
|
|
added = 0
|
|
for f in ox.sorted_strings(books):
|
|
if not state.tasks.connected:
|
|
return
|
|
position += 1
|
|
with db.session():
|
|
id = media.get_id(f)
|
|
file = File.get(id)
|
|
if not file:
|
|
file = add_file(id, f, prefix, f)
|
|
added += 1
|
|
trigger_event('change', {})
|
|
|
|
def run_import(options=None):
|
|
options = options or {}
|
|
logger.debug('run_import')
|
|
if state.activity.get('cancel'):
|
|
logger.debug('import canceled')
|
|
state.activity = {}
|
|
return
|
|
state.activity = {}
|
|
prefs = settings.preferences
|
|
prefix = os.path.expanduser(options.get('path', prefs['importPath']))
|
|
if os.path.islink(prefix):
|
|
prefix = os.path.realpath(prefix)
|
|
if not prefix[-1] == os.sep:
|
|
prefix += os.sep
|
|
prefix_books = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books' + os.sep)
|
|
prefix_imported = os.path.join(prefix_books, '.import' + os.sep)
|
|
if prefix_books.startswith(prefix) or prefix.startswith(prefix_books):
|
|
error = 'invalid path'
|
|
elif not os.path.exists(prefix):
|
|
error = 'path not found'
|
|
elif not os.path.isdir(prefix):
|
|
error = 'path must be a folder'
|
|
else:
|
|
error = None
|
|
if error:
|
|
trigger_event('activity', {
|
|
'activity': 'import',
|
|
'progress': [0, 0],
|
|
'status': {'code': 404, 'text': error}
|
|
})
|
|
state.activity = {}
|
|
return
|
|
listname = options.get('list')
|
|
if listname:
|
|
listitems = []
|
|
assert isinstance(prefix, str)
|
|
books = []
|
|
count = 0
|
|
for root, folders, files in os.walk(prefix):
|
|
for f in files:
|
|
if not state.tasks.connected:
|
|
return
|
|
#if f.startswith('._') or f == '.DS_Store':
|
|
if f.startswith('.'):
|
|
continue
|
|
f = os.path.join(root, f)
|
|
ext = f.split('.')[-1]
|
|
if ext in extensions:
|
|
books.append(f)
|
|
count += 1
|
|
if state.activity.get('cancel'):
|
|
logger.debug('active import canceled')
|
|
state.activity = {}
|
|
return
|
|
if count % 100 == 0:
|
|
state.activity = {
|
|
'activity': 'import',
|
|
'path': prefix,
|
|
'progress': [0, count],
|
|
}
|
|
trigger_event('activity', state.activity)
|
|
state.activity = {
|
|
'activity': 'import',
|
|
'path': prefix,
|
|
'progress': [0, len(books)],
|
|
}
|
|
trigger_event('activity', state.activity)
|
|
position = 0
|
|
added = 0
|
|
last = 0
|
|
for f in ox.sorted_strings(books):
|
|
position += 1
|
|
if not os.path.exists(f):
|
|
continue
|
|
with db.session():
|
|
id = media.get_id(f)
|
|
file = File.get(id)
|
|
f_import = f
|
|
if not file:
|
|
f = f.replace(prefix, prefix_imported)
|
|
ox.makedirs(os.path.dirname(f))
|
|
if options.get('mode') == 'move':
|
|
try:
|
|
shutil.move(f_import, f)
|
|
except:
|
|
shutil.copy2(f_import, f)
|
|
else:
|
|
shutil.copy2(f_import, f)
|
|
file = add_file(id, f, prefix_books, f_import)
|
|
file.move()
|
|
added += 1
|
|
elif options.get('mode') == 'move':
|
|
os.unlink(f_import)
|
|
if listname:
|
|
listitems.append(file.item.id)
|
|
if time.time() - last > 5:
|
|
last = time.time()
|
|
state.activity = {
|
|
'activity': 'import',
|
|
'progress': [position, len(books)],
|
|
'path': prefix,
|
|
'added': added,
|
|
}
|
|
trigger_event('activity', state.activity)
|
|
|
|
if state.activity.get('cancel'):
|
|
state.activity = {}
|
|
return
|
|
if not state.tasks.connected:
|
|
return
|
|
with db.session():
|
|
if listname and listitems:
|
|
l = List.get(settings.USER_ID, listname)
|
|
if l:
|
|
l.add_items(listitems)
|
|
trigger_event('activity', {
|
|
'activity': 'import',
|
|
'progress': [position, len(books)],
|
|
'path': prefix,
|
|
'status': {'code': 200, 'text': ''},
|
|
'added': added,
|
|
})
|
|
state.activity = {}
|
|
remove_empty_folders(prefix_books)
|
|
if options.get('mode') == 'move':
|
|
remove_empty_folders(prefix, True)
|
|
|
|
def import_folder():
|
|
if not (state.activity and state.activity.get('activity') == 'import'):
|
|
import_path = settings.preferences['importPath']
|
|
logger.debug('scan importPath %s', import_path)
|
|
if os.path.exists(import_path):
|
|
run_import({
|
|
'path': import_path,
|
|
'mode': 'move'
|
|
})
|
|
remove_empty_folders(import_path, True)
|
|
if state.main:
|
|
state.main.call_later(10*60, lambda: state.tasks.queue('scanimport'))
|