]> Softwares of Agnibho - medscript.git/blob - filehandler.py
Bigfix: stylesheet
[medscript.git] / filehandler.py
1 # MedScript
2 # Copyright (C) 2023 Dr. Agnibho Mondal
3 # This file is part of MedScript.
4 # MedScript is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
7
8 import os, shutil, glob, tempfile, json
9 from zipfile import ZipFile
10 from config import config
11 from signature import Signature
12
13 class FileHandler():
14
15 meta={"type":"MedScript", "version":"0.1"}
16
17 file=""
18 directory=""
19
20 def __init__(self, file=""):
21 self.reset(file)
22
23 def reset(self, file=""):
24 self.file=file
25 self.directory=tempfile.TemporaryDirectory()
26
27 def set_file(self, file):
28 self.file=file
29
30 def copy(self, file, category="attachment"):
31 dirname=os.path.join(self.directory.name, category)
32 os.makedirs(dirname, exist_ok=True)
33 try:
34 shutil.copyfile(file, os.path.join(dirname, os.path.basename(file)))
35 except shutil.SameFileError as e:
36 print(e)
37
38 def list(self, category="attachment"):
39 items=[]
40 dirname=os.path.join(self.directory.name, category)
41 for f in glob.glob(os.path.join(dirname,"*"), recursive=True):
42 items.append(f)
43 return(items)
44
45 def save(self, file=None):
46 if file is not None:
47 self.file=file
48 with open(os.path.join(self.directory.name, "meta.json"), "w") as f:
49 f.write(json.dumps(self.meta))
50 template=os.path.join(self.directory.name, "template")
51 os.makedirs(template, exist_ok=True)
52 shutil.copytree(config["template"], template, dirs_exist_ok=True)
53
54 with ZipFile(self.file, "w", strict_timestamps=False) as target:
55 for f in glob.glob(os.path.join(self.directory.name, "**" ,"*"), recursive=True):
56 target.write(f, os.path.relpath(f, self.directory.name))
57
58 def open(self, file=None):
59 if file is not None:
60 self.file=file
61 with ZipFile(self.file, "r", strict_timestamps=False) as source:
62 source.extractall(self.directory.name)
63
64 def sign(self, password=""):
65 with open(os.path.join(self.directory.name, "prescription.json"), "r") as file:
66 data=file.read()
67 signature=Signature.sign(data, certificate=config["certificate"], privkey=config["private_key"], password=password)
68 with open(os.path.join(self.directory.name, "signature.p7m"), "w") as file:
69 file.write(signature)
70 shutil.copyfile(config["certificate"], os.path.join(self.directory.name, "certificate.pem"))
71
72 def verify(self):
73 with open(os.path.join(self.directory.name, "prescription.json"), "r") as file:
74 data=file.read()
75 try:
76 with open(os.path.join(self.directory.name, "certificate.pem")) as file:
77 certificate=file.read()
78 with open(os.path.join(self.directory.name, "signature.p7m")) as file:
79 signature=file.read()
80 return Signature.verify(data, certificate=os.path.join(self.directory.name, "certificate.pem"), signature=os.path.join(self.directory.name, "signature.p7m"))
81 except FileNotFoundError as e:
82 print(e)
83
84 def delete_attachment(self, item):
85 try:
86 os.unlink(os.path.join(self.directory.name, "attachment", os.path.basename(item)))
87 except Exception as e:
88 print(e)
89
90 def delete_sign(self):
91 try:
92 os.unlink(os.path.join(self.directory.name, "certificate.pem"))
93 os.unlink(os.path.join(self.directory.name, "signature.p7m"))
94 except Exception as e:
95 print(e)
96
97 def is_signed(self):
98 return(os.path.exists(os.path.join(self.directory.name, "certificate.pem")) and (os.path.exists(os.path.join(self.directory.name, "signature.p7m"))))