-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirserver.py
More file actions
executable file
·116 lines (103 loc) · 3.54 KB
/
dirserver.py
File metadata and controls
executable file
·116 lines (103 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/local/bin/python3
# general
from pprint import pprint
# flask
from flask_restful import marshal
# my utils
import my_errors
my_errors.make_classes(my_errors.errors)
import my_fields
import check
# --- security ---
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
import decrypt_message
import send_securily
# --- mongo ----
from pymongo import MongoClient
from bson.objectid import ObjectId
import mongo_stuff
class dirServer():
"""
Server providing file locations to clients
"""
def __init__(self,
secret_key='the quick brown fox jumps over the lazy dog'):
self.load_machines()
self.load_files()
self.s = Serializer(secret_key)
# util functions
def load_machines(self):
"""Load persistnet storage of registered machines"""
self.db_machines = MongoClient().test_database.db.machines
# drop db for testing, will not be in deployed version
self.db_machines.drop()
# print(self.db_machines)
return True
def load_files(self):
"""Load persistnet storage of registered files"""
self.db_files = MongoClient().test_database.db.machine_files
# drop db for testing, will not be in deployed version
self.db_files.drop()
# print(self.db_files)
return True
# registration
@check.reqs(['name', 'machine_id', 'uri'])
def register_file(self, **kwargs):
"""Register a new files locations"""
# print('in reg')
f = {k: v for k, v in kwargs.items()}
Id = mongo_stuff.insert(self.db_files, f)
return self.get_file(Id)
@check.reqs(['callback'])
def register_machine(self, **kwargs):
"""Register a new machine"""
m = {k: v for k, v in kwargs.items()}
r = mongo_stuff.insert_or_override(self.db_machines, m)
return r
@check.reqs(['_id'])
def unreg_file(self, *args, **kwargs):
"""Unregester a file (delete its location)"""
# print('in unreg file')
kwargs['_id'] = ObjectId(kwargs['_id'])
return bool(self.db_files.delete_one(kwargs).deleted_count)
@check.reqs(['machine_id'])
def unreg_machine(self, machine_id):
"""Unregsiter a machine (delete all its files)"""
# print('in unreg machine')
self.db_files.delete_many({'machine_id': machine_id})
return bool(
self.db_machines.delete_many({
'_id': ObjectId(machine_id)
}).deleted_count)
def get_file(self, Id):
"""Retreive the location of a file by file _id"""
# print('in get file')
f = self.db_files.find_one({'_id': ObjectId(Id)})
if f:
return f
else:
raise my_errors.not_found
def get_machine(self, Id):
"""Retreive machine information by machine _id"""
# print('in get machine')
m = self.db_machines.find_one({'_id': ObjectId(Id)})
if m:
return m
else:
raise my_errors.not_found
@send_securily.with_token
@decrypt_message.with_token
@check.reqs(['name'])
def search_filename(self, *args, **kwargs):
"""Search for file by name, and return list of matching files"""
# print('in search')
files = self.db_files.find(kwargs)
if files:
_files = [f for f in files]
return {
'files':
[marshal(f, my_fields.dir_file_fields) for f in _files]
}
else:
# print("file name not found")
raise my_errors.not_found