import books from import folder, fixes #136
This commit is contained in:
parent
1cd6f35b86
commit
75fd720b19
4 changed files with 32 additions and 11 deletions
|
@ -10,7 +10,7 @@ import time
|
|||
import ox
|
||||
|
||||
from changelog import Changelog
|
||||
from item.models import File, Scrape
|
||||
from item.models import File
|
||||
from user.models import List
|
||||
from utils import remove_empty_folders
|
||||
from websocket import trigger_event
|
||||
|
@ -47,7 +47,7 @@ def remove_missing():
|
|||
if not state.tasks.connected:
|
||||
return
|
||||
f.move()
|
||||
remove_empty_folders(prefix)
|
||||
remove_empty_folders(prefix, True)
|
||||
|
||||
def add_file(id, f, prefix, from_=None):
|
||||
user = state.user()
|
||||
|
@ -173,19 +173,24 @@ def run_import(options=None):
|
|||
with db.session():
|
||||
id = media.get_id(f)
|
||||
file = File.get(id)
|
||||
f_import = f
|
||||
if not file:
|
||||
f_import = f
|
||||
f = f.replace(prefix, prefix_imported)
|
||||
ox.makedirs(os.path.dirname(f))
|
||||
if options.get('mode') == 'move':
|
||||
shutil.move(f_import, f)
|
||||
try:
|
||||
shutil.move(f_import, f)
|
||||
except:
|
||||
shutil.copy2(f_import, f)
|
||||
else:
|
||||
shutil.copy(f_import, f)
|
||||
shutil.copy2(f_import, f)
|
||||
file = add_file(id, f, prefix_books, f_import)
|
||||
file.move()
|
||||
added += 1
|
||||
if listname:
|
||||
listitems.append(file.item.id)
|
||||
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 = {
|
||||
|
@ -214,4 +219,16 @@ def run_import(options=None):
|
|||
state.activity = {}
|
||||
remove_empty_folders(prefix_books)
|
||||
if options.get('mode') == 'move':
|
||||
remove_empty_folders(prefix)
|
||||
remove_empty_folders(prefix, True)
|
||||
|
||||
def import_folder():
|
||||
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'))
|
||||
|
|
|
@ -159,6 +159,7 @@ def run():
|
|||
state.main.call_later(10, publish)
|
||||
else:
|
||||
nodes.publish_node()
|
||||
state.main.call_later(10, lambda: state.tasks.queue('scanimport'))
|
||||
state.main.add_callback(publish)
|
||||
|
||||
if not state.update:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
|
||||
import os
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
|
||||
|
@ -36,6 +36,8 @@ class Tasks(Thread):
|
|||
export_list(data)
|
||||
elif action == 'scan':
|
||||
item.scan.run_scan()
|
||||
elif action == 'scanimport':
|
||||
item.scan.import_folder()
|
||||
elif action == 'peering':
|
||||
update_user_peering(*data)
|
||||
else:
|
||||
|
|
|
@ -310,14 +310,15 @@ def update_dict(root, data):
|
|||
if hasattr(root, '_save'):
|
||||
root._save()
|
||||
|
||||
def remove_empty_folders(prefix):
|
||||
def remove_empty_folders(prefix, keep_root=False):
|
||||
empty = []
|
||||
for root, folders, files in os.walk(prefix):
|
||||
if len(files) == 1 and files[0] == '.DS_Store':
|
||||
os.unlink(os.path.join(root, files[0]))
|
||||
files = []
|
||||
if not folders and not files:
|
||||
empty.append(root)
|
||||
if root != prefix or not keep_root:
|
||||
empty.append(root)
|
||||
for folder in empty:
|
||||
remove_empty_tree(folder)
|
||||
|
||||
|
|
Loading…
Reference in a new issue