File tree Expand file tree Collapse file tree 1 file changed +14
-12
lines changed Expand file tree Collapse file tree 1 file changed +14
-12
lines changed Original file line number Diff line number Diff line change 25
25
raise OSError ("Please create a /static directory on the CIRCUITPY drive." ) from e
26
26
27
27
28
+ def is_file (path : str ):
29
+ return (os .stat (path .rstrip ("/" ))[0 ] & 0b_11110000_00000000 ) == 0b_10000000_00000000
30
+
31
+
28
32
@server .route ("/" )
29
33
def directory_listing (request : Request ):
30
34
path = request .query_params .get ("path" , "" ).replace ("%20" , " " )
31
35
32
36
# Preventing path traversal by removing all ../ from path
33
37
path = re .sub (r"\/(\.\.)\/|\/(\.\.)|(\.\.)\/" , "/" , path ).strip ("/" )
34
38
35
- if path :
36
- is_file = (
37
- os .stat (f"/static/{ path } " )[0 ] & 0b_11110000_00000000
38
- ) == 0b_10000000_00000000
39
- else :
40
- is_file = False
41
-
42
39
# If path is a file, return it as a file response
43
- if is_file :
40
+ if is_file ( f"/static/ { path } " ) :
44
41
return FileResponse (request , path )
45
42
43
+ items = sorted (
44
+ [
45
+ item + ("" if is_file (f"/static/{ path } /{ item } " ) else "/" )
46
+ for item in os .listdir (f"/static/{ path } " )
47
+ ],
48
+ key = lambda item : not item .endswith ("/" ),
49
+ )
50
+
46
51
# Otherwise, return a directory listing
47
52
return Response (
48
53
request ,
49
54
render_template (
50
55
"directory_listing.tpl.html" ,
51
- context = {
52
- "path" : path ,
53
- "items" : os .listdir (f"/static/{ path } " ),
54
- },
56
+ context = {"path" : path , "items" : items },
55
57
),
56
58
content_type = "text/html" ,
57
59
)
You can’t perform that action at this time.
0 commit comments