Spaces:
Sleeping
Sleeping
Emerson-Britto
commited on
Commit
·
af7c474
1
Parent(s):
1db582e
[uo]
Browse files- __pycache__/fileManager.cpython-39.pyc +0 -0
- app.py +13 -3
- collector_data.json +1 -0
- fileManager.py +148 -0
- fl/hi.txt +0 -0
__pycache__/fileManager.cpython-39.pyc
ADDED
Binary file (5.02 kB). View file
|
|
app.py
CHANGED
@@ -1,7 +1,17 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
nxapp.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from uuid import uuid4
|
3 |
+
from fileManager import FileManager
|
4 |
+
import re
|
5 |
|
6 |
+
fileManager = FileManager()
|
7 |
+
fileManager.start()
|
8 |
|
9 |
+
|
10 |
+
def saveFile(file):
|
11 |
+
with open(file.name, "rb") as rfile:
|
12 |
+
file_infor = fileManager.saveFile(rfile.read(), file.orig_name)
|
13 |
+
return f"https://emee-nx-storage.hf.space/file=./{file_infor['path']}"
|
14 |
+
|
15 |
+
|
16 |
+
nxapp = gr.Interface(fn=saveFile, inputs="file", outputs="text")
|
17 |
nxapp.launch()
|
collector_data.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
[]
|
fileManager.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from threading import Thread
|
2 |
+
from copy import deepcopy
|
3 |
+
from uuid import uuid4
|
4 |
+
from time import sleep
|
5 |
+
import json
|
6 |
+
import time
|
7 |
+
import re
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
def syncmethod(func):
|
12 |
+
def function(*args, **kwargs):
|
13 |
+
th = Thread(target=func, args=args, kwargs=kwargs)
|
14 |
+
th.start()
|
15 |
+
return function
|
16 |
+
|
17 |
+
|
18 |
+
@syncmethod
|
19 |
+
def interval(fc, time):
|
20 |
+
while True:
|
21 |
+
sleep(time)
|
22 |
+
fc()
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
class FileManager:
|
28 |
+
def __init__(self):
|
29 |
+
super(FileManager, self).__init__()
|
30 |
+
self.data_file = "collector_data.json"
|
31 |
+
# self.base_url = "http://localhost:7860"
|
32 |
+
self.base_url = "https://emee-nexa.hf.space"
|
33 |
+
if not os.path.exists(self.data_file): self.writeJson()
|
34 |
+
else: self.checkFiles()
|
35 |
+
|
36 |
+
|
37 |
+
def formatPath(self, path):
|
38 |
+
new_path = re.sub(r'\s+|\s', '-', path)
|
39 |
+
new_path = re.sub(r'\!|\?|\#|\*|Ç|ç|ê|Ê|ã|Ã', '', new_path)
|
40 |
+
return new_path
|
41 |
+
|
42 |
+
|
43 |
+
def rename(self, path, new_path):
|
44 |
+
os.rename(path, new_path)
|
45 |
+
|
46 |
+
|
47 |
+
def readJson(self, path):
|
48 |
+
with open(path, 'r') as json_data:
|
49 |
+
return json.load(json_data)
|
50 |
+
|
51 |
+
|
52 |
+
def writeJson(self, data={}):
|
53 |
+
data_file = open(self.data_file, "w")
|
54 |
+
json.dump(data, data_file, indent=2)
|
55 |
+
data_file.close()
|
56 |
+
|
57 |
+
|
58 |
+
def start(self, gap=5):
|
59 |
+
interval(self.checkFiles, gap * 60)
|
60 |
+
|
61 |
+
|
62 |
+
def checkFiles(self):
|
63 |
+
data_file = self.readJson(self.data_file)
|
64 |
+
for idx, file_infor in enumerate(deepcopy(data_file)):
|
65 |
+
if file_infor["expiresAt"] < time.time():
|
66 |
+
try:
|
67 |
+
os.remove(file_infor["path"])
|
68 |
+
except Exception as e:
|
69 |
+
print(e)
|
70 |
+
del data_file[idx]
|
71 |
+
self.writeJson(data=data_file)
|
72 |
+
|
73 |
+
|
74 |
+
def reValidate(self, path, expiresAt=60):
|
75 |
+
data_file = self.readJson(self.data_file)
|
76 |
+
for file_infor in data_file:
|
77 |
+
if file_infor["path"] == path:
|
78 |
+
file_infor["expiresAt"] = time.time() + (expiresAt * 60)
|
79 |
+
self.writeJson(data=data_file)
|
80 |
+
|
81 |
+
|
82 |
+
def addPath(self, path, expiresAt=60):
|
83 |
+
new_path = self.formatPath(path)
|
84 |
+
self.rename(path, new_path)
|
85 |
+
data_file = self.readJson(self.data_file)
|
86 |
+
fc = lambda file_infor: file_infor["path"] != new_path
|
87 |
+
data_file = list(filter(fc, data_file))
|
88 |
+
new_file_infor = {
|
89 |
+
"path": new_path,
|
90 |
+
"filename": new_path.split("/")[-1],
|
91 |
+
"id": str(uuid4()),
|
92 |
+
"expiresAt": time.time() + (expiresAt * 60) # + 30 minutes
|
93 |
+
}
|
94 |
+
data_file.append(new_file_infor)
|
95 |
+
self.writeJson(data=data_file)
|
96 |
+
return new_file_infor
|
97 |
+
|
98 |
+
|
99 |
+
def hasFile(self, path):
|
100 |
+
path = self.formatPath(path)
|
101 |
+
data_file = self.readJson(self.data_file)
|
102 |
+
for file_infor in data_file:
|
103 |
+
if file_infor["path"] == path:
|
104 |
+
self.reValidate(path)
|
105 |
+
return True
|
106 |
+
|
107 |
+
|
108 |
+
def saveFile(self, data, filename):
|
109 |
+
filename = self.formatPath(filename)
|
110 |
+
file_path = f"fl/{str(uuid4())}_{filename}"
|
111 |
+
with open(file_path, "wb") as file:
|
112 |
+
file.write(data)
|
113 |
+
return self.addPath(file_path)
|
114 |
+
|
115 |
+
|
116 |
+
def getFileObjByPath(self, path):
|
117 |
+
path = self.formatPath(path)
|
118 |
+
data_file = self.readJson(self.data_file)
|
119 |
+
for file_infor in data_file:
|
120 |
+
if file_infor["path"] == path:
|
121 |
+
self.reValidate(path)
|
122 |
+
return file_infor
|
123 |
+
|
124 |
+
|
125 |
+
def getFileObjById(self, uid):
|
126 |
+
data_file = self.readJson(self.data_file)
|
127 |
+
for file_infor in data_file:
|
128 |
+
if file_infor["id"] == uid:
|
129 |
+
self.reValidate(file_infor["path"])
|
130 |
+
return file_infor
|
131 |
+
|
132 |
+
|
133 |
+
def getFileById(self, uid):
|
134 |
+
file_path = self.getFilePathById(uid)
|
135 |
+
if not file_path: return
|
136 |
+
with open(file_path, "rb") as file:
|
137 |
+
file_data = file.read()
|
138 |
+
return file_data
|
139 |
+
|
140 |
+
|
141 |
+
def getFileUrlById(self, uid):
|
142 |
+
file_path = self.getFilePathById(uid)
|
143 |
+
if not file_path: return
|
144 |
+
return self.getFileUrlByPath(file_path)
|
145 |
+
|
146 |
+
|
147 |
+
def getFileUrlByPath(self, path):
|
148 |
+
return f"{self.base_url}/file=./{path}"
|
fl/hi.txt
ADDED
File without changes
|