-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.py
103 lines (88 loc) · 2.99 KB
/
resource.py
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
import os
import os.path as o
import mimetypes
import datetime
import base64
from shutil import rmtree
from urllib import quote, unquote
class Resource:
def initFromUrl(self, url):
self.url = url
# use path
path = urlToPath(url).rstrip('/')
assert(o.exists(path))
self.name = o.basename(path)
# use normpath
path = o.normpath(path).rstrip('/')
self.path = path
self.category = 'directory' if o.isdir(path) else 'file'
if(self.category == 'directory'):
self.numItems = len(os.listdir(path))
else:
self.size = o.getsize(path)
self.resourceDate = datetime.datetime.utcfromtimestamp(o.getmtime(path))
self.resourceType = mimetypes.guess_type(path)[0]
def initFromPath(self, path):
# use path
if o.exists(path):
self.name = o.basename(path)
# use normpath
path = o.normpath(path).rstrip('/')
self.path = path
self.category = 'directory' if o.isdir(path) else 'file'
if(self.category == 'directory'):
self.numItems = len(os.listdir(path))
else:
self.size = o.getsize(path)
self.resourceDate = datetime.datetime.utcfromtimestamp(o.getmtime(path))
self.resourceType = mimetypes.guess_type(path)[0]
def addPath(self):
self.path = o.normpath(urlToPath(self.url).rstrip('/')).rstrip('/')
def addContent(self, path=False):
if path == False:
path = self.path
self.encoding = "Base64"
file = open(path, "rb")
self.content = base64.b64encode(file.read())
file.close()
def putContent(self, path):
if self.category == 'directory':
os.mkdir(path)
else:
if not self.encoding == "Base64":
raise Exception("Only Base64 encoding is supported.")
file = open(path, "wb")
file.write(base64.b64decode(self.content))
file.close()
def deleteContent(self):
if self.category == "file":
os.remove(self.path)
else:
rmtree(self.path)
def urlToPath(url):
return unquote(url.split('/',3)[3])
def splitUrl(url):
lame_path = url.split('/',3)[3]
path = urlToPath(url)
front = url[0: len(url) - len(lame_path) - 1]
return (front, path)
def getResourceList(url):
url = url.rstrip('/')
(front, path) = splitUrl(url)
path = unquote(path)
path = o.normpath(path)
assert(o.isdir(path))
resourceList = []
files = os.listdir(path)
if not o.dirname(path) == '':
back = Resource()
backURL = front + '/' + quote(o.normpath(path + '/' + '..'))
back.initFromUrl(backURL)
back.name = '..'
resourceList.append(back)
for file in files:
file = quote(path) +'/'+ quote(file)
r = Resource()
r.initFromUrl(front + '/' + file)
resourceList.append(r)
return resourceList