add peering task
This commit is contained in:
parent
c517922fb9
commit
f6cfb255a6
2 changed files with 88 additions and 80 deletions
|
@ -22,7 +22,7 @@ class Tasks(Thread):
|
|||
|
||||
def run(self):
|
||||
import item.scan
|
||||
from user.models import List
|
||||
from user.models import export_list, update_user_peering
|
||||
while self.connected:
|
||||
m = self.q.get()
|
||||
if m:
|
||||
|
@ -33,9 +33,11 @@ class Tasks(Thread):
|
|||
elif action == 'import':
|
||||
item.scan.run_import(data)
|
||||
elif action == 'export':
|
||||
List.export(data)
|
||||
export_list(data)
|
||||
elif action == 'scan':
|
||||
item.scan.run_scan()
|
||||
elif action == 'peering':
|
||||
update_user_peering(*data)
|
||||
else:
|
||||
trigger_event('error', {'error': 'unknown action'})
|
||||
except:
|
||||
|
|
|
@ -331,84 +331,6 @@ class List(db.Model):
|
|||
def create_symlinks(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def export(cls, data):
|
||||
with db.session():
|
||||
self = cls.get(data['list'])
|
||||
if not self:
|
||||
return
|
||||
mode = data.get('mode')
|
||||
prefix = data.get('path')
|
||||
if mode not in ('add', 'replace'):
|
||||
logger.debug('invalid mode %s', mode)
|
||||
return
|
||||
if not prefix or prefix == '/':
|
||||
logger.debug('invalid export path %s', prefix)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [0, 0],
|
||||
'status': {'code': 404, 'text': 'invalid export path'}
|
||||
})
|
||||
return
|
||||
root = prefix
|
||||
while not os.path.exists(root) and root != '/':
|
||||
root = os.path.dirname(root)
|
||||
if not os.access(root, os.W_OK):
|
||||
logger.debug('can not write to %s', root)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [0, 0],
|
||||
'path': prefix,
|
||||
'status': {'code': 404, 'text': 'permission denied'}
|
||||
})
|
||||
return
|
||||
if os.path.exists(prefix):
|
||||
existing_files = set(
|
||||
os.path.join(root, f) for root, _, files in os.walk(prefix) for f in files
|
||||
)
|
||||
else:
|
||||
existing_files = set()
|
||||
new_files = set()
|
||||
count = self.get_items().count()
|
||||
n = 1
|
||||
for i in self.get_items():
|
||||
if i.files.all():
|
||||
f = i.files.all()[0]
|
||||
source = f.fullpath()
|
||||
target = os.path.join(prefix, f.path)
|
||||
if mode == 'add':
|
||||
p = 1
|
||||
parts = target.rsplit('.', 1)
|
||||
while os.path.exists(target) and media.get_id(target) != f.sha1:
|
||||
target = '.'.join([parts[0], f.sha1[:p], parts[1]])
|
||||
p += 1
|
||||
ox.makedirs(os.path.dirname(target))
|
||||
if os.path.exists(target):
|
||||
if mode == 'replace' and media.get_id(target) != f.sha1:
|
||||
os.unlink(target)
|
||||
shutil.copy2(source, target)
|
||||
else:
|
||||
shutil.copy2(source, target)
|
||||
new_files.add(target)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [n, count]
|
||||
})
|
||||
n += 1
|
||||
if mode == 'replace':
|
||||
for f in list(existing_files - new_files):
|
||||
os.unlink(f)
|
||||
utils.remove_empty_folders(prefix)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'progress': [count, count],
|
||||
'path': prefix,
|
||||
'status': {'code': 200, 'text': ''},
|
||||
})
|
||||
|
||||
class Metadata(db.Model):
|
||||
__tablename__ = 'user_metadata'
|
||||
|
||||
|
@ -476,3 +398,87 @@ class Metadata(db.Model):
|
|||
def delete(self):
|
||||
state.db.session.delete(self)
|
||||
state.db.session.commit()
|
||||
|
||||
def export_list(data):
|
||||
with db.session():
|
||||
self = List.get(data['list'])
|
||||
if not self:
|
||||
return
|
||||
mode = data.get('mode')
|
||||
prefix = data.get('path')
|
||||
if mode not in ('add', 'replace'):
|
||||
logger.debug('invalid mode %s', mode)
|
||||
return
|
||||
if not prefix or prefix == '/':
|
||||
logger.debug('invalid export path %s', prefix)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [0, 0],
|
||||
'status': {'code': 404, 'text': 'invalid export path'}
|
||||
})
|
||||
return
|
||||
root = prefix
|
||||
while not os.path.exists(root) and root != '/':
|
||||
root = os.path.dirname(root)
|
||||
if not os.access(root, os.W_OK):
|
||||
logger.debug('can not write to %s', root)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [0, 0],
|
||||
'path': prefix,
|
||||
'status': {'code': 404, 'text': 'permission denied'}
|
||||
})
|
||||
return
|
||||
if os.path.exists(prefix):
|
||||
existing_files = set(
|
||||
os.path.join(root, f) for root, _, files in os.walk(prefix) for f in files
|
||||
)
|
||||
else:
|
||||
existing_files = set()
|
||||
new_files = set()
|
||||
count = self.get_items().count()
|
||||
n = 1
|
||||
for i in self.get_items():
|
||||
if i.files.all():
|
||||
f = i.files.all()[0]
|
||||
source = f.fullpath()
|
||||
target = os.path.join(prefix, f.path)
|
||||
if mode == 'add':
|
||||
p = 1
|
||||
parts = target.rsplit('.', 1)
|
||||
while os.path.exists(target) and media.get_id(target) != f.sha1:
|
||||
target = '.'.join([parts[0], f.sha1[:p], parts[1]])
|
||||
p += 1
|
||||
ox.makedirs(os.path.dirname(target))
|
||||
if os.path.exists(target):
|
||||
if mode == 'replace' and media.get_id(target) != f.sha1:
|
||||
os.unlink(target)
|
||||
shutil.copy2(source, target)
|
||||
else:
|
||||
shutil.copy2(source, target)
|
||||
new_files.add(target)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'path': prefix,
|
||||
'progress': [n, count]
|
||||
})
|
||||
n += 1
|
||||
if mode == 'replace':
|
||||
for f in list(existing_files - new_files):
|
||||
os.unlink(f)
|
||||
utils.remove_empty_folders(prefix)
|
||||
trigger_event('activity', {
|
||||
'activity': 'export',
|
||||
'progress': [count, count],
|
||||
'path': prefix,
|
||||
'status': {'code': 200, 'text': ''},
|
||||
})
|
||||
|
||||
def update_user_peering(user_id, peered, username=None):
|
||||
with db.session():
|
||||
u = User.get(user_id)
|
||||
if u:
|
||||
u.update_peering(peered, username)
|
||||
|
||||
|
|
Loading…
Reference in a new issue