double check that volume is still around, only flag files as missing

This commit is contained in:
j 2019-02-13 12:36:25 +05:30
parent ee315399bb
commit 8928b5447b
3 changed files with 66 additions and 14 deletions

View file

@ -556,6 +556,11 @@ class Item(db.Model):
return True return True
return False return False
def missing_file(self):
logger.debug('file is missing! %s: %s', self.id, self.get_path())
self.info['missing'] = True
state.db.session.add(self)
def remove_file(self): def remove_file(self):
for f in self.files.all(): for f in self.files.all():
path = f.fullpath() path = f.fullpath()

View file

@ -29,10 +29,11 @@ def remove_missing(books=None):
dirty = False dirty = False
logger.debug('remove missing') logger.debug('remove missing')
prefix = get_prefix() prefix = get_prefix()
oml_prefix = os.path.dirname(prefix)
if books is None: if books is None:
books = collect_books(prefix) books = collect_books(prefix)
with db.session(): with db.session():
if os.path.exists(prefix): if os.path.exists(prefix) and os.path.exists(oml_prefix):
logger.debug('scan for removed files') logger.debug('scan for removed files')
db_paths = [] db_paths = []
items = {} items = {}
@ -44,29 +45,35 @@ def remove_missing(books=None):
if f.item: if f.item:
items[path] = f.sha1 items[path] = f.sha1
else: else:
logger.debug('remove orphaned file %s', f)
state.db.session.delete(f) state.db.session.delete(f)
dirty = True dirty = True
if dirty: if dirty:
state.db.session.commit() state.db.session.commit()
dirty = False dirty = False
nfd_books = {unicodedata.normalize('NFD', path) for path in books} nfc_books = {unicodedata.normalize('NFC', path) for path in books}
removed = [ removed = [
path for path in db_paths path for path in db_paths
if unicodedata.normalize('NFD', path) not in nfd_books if unicodedata.normalize('NFC', path) not in nfc_books
] ]
if removed: if removed and os.path.exists(prefix) and os.path.exists(oml_prefix):
logger.debug('%s files removed', len(removed)) logger.debug('%s files removed', len(removed))
ids = [items[path] for path in removed] ids = [items[path] for path in removed]
orphaned = set(ids) if ids:
for i in Item.query.filter(Item.id.in_(ids)): orphaned = set(ids)
i.remove_file() for i in Item.query.filter(Item.id.in_(ids)):
orphaned.remove(i.id) if state.shutdown:
dirty = True continue
if orphaned: i.missing_file()
logger.debug('%s files orphaned', len(orphaned)) orphaned.remove(i.id)
for f in File.query.filter(File.sha1.in_(orphaned)): dirty = True
state.db.session.delete(f) if orphaned:
dirty = True logger.debug('%s files orphaned', len(orphaned))
for f in File.query.filter(File.sha1.in_(orphaned)):
if state.shutdown:
continue
state.db.session.delete(f)
dirty = True
if dirty: if dirty:
state.db.session.commit() state.db.session.commit()
state.cache.clear('group:') state.cache.clear('group:')
@ -182,6 +189,10 @@ def run_scan():
add_record('edititem', item.id, item.meta) add_record('edititem', item.id, item.meta)
item.update() item.update()
added += 1 added += 1
if file and file.item.info.get('missing'):
logger.debug('missing file showed up again %s: %s', id, file.fullpath())
del file.item.info['missing']
file.item.save()
if file and not file.item.added: if file and not file.item.added:
file.item.added = datetime.utcnow() file.item.added = datetime.utcnow()
if file.item.accessed: if file.item.accessed:

View file

@ -256,6 +256,42 @@ oml.ui.infoView = function(externalData, isMixed) {
], ],
float: 'right' float: 'right'
}) })
: data.missing
? Ox.FormElementGroup({
elements: [
Ox.Button({
style: 'squared',
title: Ox._('Book Missing'),
width: 112
})
.bindEvent({
click: function() {
if (!oml.readOnly) {
oml.api.openFolder({id: oml.user.ui.item});
}
}
}),
Ox.MenuButton({
items: [
].concat(oml.readOnly ? [] : [
{id: 'show', title: Ox._('Show File')}
]),
overlap: 'left',
style: 'squared',
title: 'select',
tooltip: Ox._('File was removed'),
type: 'image'
})
.bindEvent({
click: function(data_) {
if (!oml.readOnly) {
oml.api.openFolder({id: oml.user.ui.item});
}
}
})
],
float: 'right'
})
: Ox.FormElementGroup({ : Ox.FormElementGroup({
elements: [ elements: [
Ox.Button({ Ox.Button({