-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInterface.py
More file actions
74 lines (55 loc) · 2.21 KB
/
FileInterface.py
File metadata and controls
74 lines (55 loc) · 2.21 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
import os
import json
import base64
from glob import glob
class FileInterface:
def __init__(self):
os.chdir('uploads/')
def list(self, params=[]):
try:
filelist = glob('*.*')
return dict(status='OK',data=filelist)
except Exception as e:
return dict(status='ERROR',data=str(e))
def get(self, params=[]):
try:
filename = params[0]
if (filename == ''):
return None
fp = open(f"{filename}", 'rb')
isifile = base64.b64encode(fp.read()).decode()
return dict(status='OK', data_namafile=filename, data_file=isifile)
except Exception as e:
return dict(status='ERROR', data=str(e))
def add(self, params=[]):
try:
paramsCount = len(params)
filename = params[0]
if (paramsCount < 2 or filename == ''):
return dict(status='ERROR', data="Parameter tidak lengkap")
fileContent = base64.b64decode(params[1])
file = open(filename, 'wb+')
file.write(fileContent)
file.close()
checkFileWritten = os.path.exists(filename)
if checkFileWritten:
return dict(status='OK', data=f"File {filename} berhasil diupload")
else:
return dict(status='ERROR', data=f"File {filename} gagal diupload")
except Exception as e:
return dict(status='ERROR', data=str(e))
def delete(self, params=[]):
try:
filename = params[0]
if (filename == ''):
return dict(status='ERROR', data="Parameter tidak lengkap")
os.remove(filename)
checkFileDeleted = os.path.exists(filename)
if checkFileDeleted:
return dict(status='ERROR', data=f"File {filename} gagal dihapus")
return dict(status='OK', data=f"File {filename} berhasil dihapus")
except Exception as e:
return dict(status='ERROR', data=str(e))
if __name__=='__main__':
f = FileInterface()
print(f.list())