Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 9ae0a4e

Browse files
committed
added ui/storage show all files vs. filter by account id
1 parent b392f7a commit 9ae0a4e

File tree

4 files changed

+86
-63
lines changed

4 files changed

+86
-63
lines changed

database/mongo/storage.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ func (mg *Mongo) DeleteFile(dbName, fileID string) error {
112112
func (mg *Mongo) ListAllFiles(dbName, accountID string) ([]internal.File, error) {
113113
db := mg.Client.Database(dbName)
114114

115-
aid, err := primitive.ObjectIDFromHex(accountID)
116-
if err != nil {
117-
return nil, err
118-
}
115+
var filter bson.M
119116

120-
filter := bson.M{FieldAccountID: aid}
117+
if len(accountID) > 0 {
118+
aid, err := primitive.ObjectIDFromHex(accountID)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
filter = bson.M{FieldAccountID: aid}
124+
}
121125

122126
sr, err := db.Collection("sb_files").Find(mg.Ctx, filter)
123127
if err != nil {

database/postgresql/storage.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ func (pg *PostgreSQL) DeleteFile(dbName, fileID string) error {
5151
}
5252

5353
func (pg *PostgreSQL) ListAllFiles(dbName, accountID string) (results []internal.File, err error) {
54+
where := "WHERE account_id = $1"
55+
56+
// if no accountID is specify, the admin UI
57+
// display all files uploaded.
58+
if len(accountID) == 0 {
59+
where = "WHERE $1 = $1"
60+
}
61+
5462
qry := fmt.Sprintf(`
5563
SELECT *
5664
FROM %s.sb_files
57-
WHERE account_id = $1
58-
`, dbName)
65+
%s
66+
`, dbName, where)
5967

6068
rows, err := pg.DB.Query(qry, accountID)
6169
if err != nil {

templates/fs_list.html

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,77 @@
11
{{ template "head" .}}
22

33
<body>
4-
{{template "navbar" .}}
5-
<div class="container p-6">
6-
<br>
7-
<h2 class="title is-2">
8-
Files
9-
</h2>
10-
<table class="table is-striped" style="width:100%;">
11-
<thead>
12-
<tr>
13-
<th style="width:30%;">Name</th>
14-
<th style="width:10%; text-align:center;">Size</th>
15-
<th style="width:10%; text-align:center;">Type</th>
16-
<th style="width:20%; text-align:center;">Uploaded</th>
17-
<th style="text-align:center;">Preview</th>
18-
<th style="text-align:center;">Delete</th>
19-
</tr>
20-
</thead>
21-
<tbody>
22-
{{range .Data}}
23-
{{$fileType := convertFileType .Key}}
24-
{{$elementType := getElementByFileExt $fileType}}
25-
26-
{{ if or (eq $elementType "") (eq $elementType "audio")}}
27-
<tr style="height:100px;">
28-
{{ else }}
4+
{{template "navbar" .}}
5+
<div class="container p-6">
6+
<div class="columns py-4">
7+
<div class="column">
8+
<h2 class="title is-2">
9+
Files
10+
</h2>
11+
</div>
12+
<div class="column">
13+
<form action="/ui/fs" method="get">
14+
<div class="control">
15+
<input type="text" name="id" id="id"
16+
class="input" placeholder="Enter an account ID">
17+
</div>
18+
</form>
19+
</div>
20+
</div>
21+
22+
<table class="table is-striped" style="width:100%;">
23+
<thead>
24+
<tr>
25+
<th style="width:30%;">Name</th>
26+
<th style="width:10%; text-align:center;">Size</th>
27+
<th style="width:10%; text-align:center;">Type</th>
28+
<th style="width:20%; text-align:center;">Uploaded</th>
29+
<th style="text-align:center;">Preview</th>
30+
<th style="text-align:center;">Delete</th>
31+
</tr>
32+
</thead>
33+
<tbody>
34+
{{range .Data}}
35+
{{$fileType := convertFileType .Key}}
36+
{{$elementType := getElementByFileExt $fileType}}
37+
38+
{{ if or (eq $elementType "") (eq $elementType "audio")}}
39+
<tr style="height:100px;">
40+
{{ else }}
2941
<tr style="height:200px;">
30-
{{ end }}
31-
<td>{{ parseFilename .Key }}</td>
32-
<td style="text-align:center;">{{ convertFileSize .Size }}</td>
33-
<td style="text-align:center;">{{ $fileType }}</td>
34-
<td style="text-align:center;">{{ convertFileUploadedDate .Uploaded}}</td>
35-
<td style="text-align:center;">
36-
{{ if eq $elementType "image" }}
42+
{{ end }}
43+
<td>{{ parseFilename .Key }}</td>
44+
<td style="text-align:center;">{{ convertFileSize .Size }}</td>
45+
<td style="text-align:center;">{{ $fileType }}</td>
46+
<td style="text-align:center;">{{ convertFileUploadedDate .Uploaded}}</td>
47+
<td style="text-align:center;">
48+
{{ if eq $elementType "image" }}
3749
<a href="{{ .URL }}">
3850
<img src="{{ .URL }}" alt="uploaded image" style="height:200px;">
39-
</a>
40-
{{ else if eq $elementType "video" }}
41-
<video controls style="height:200px;">
51+
</a>
52+
{{ else if eq $elementType "video" }}
53+
<video controls style="height:200px;">
4254
<source src="{{ .URL }}">
4355
</video>
44-
{{ else if eq $elementType "audio" }}
56+
{{ else if eq $elementType "audio" }}
4557
<audio src="{{ .URL }}" controls></audio>
46-
{{ else }}
58+
{{ else }}
4759
<a href="{{ .URL }}">
4860
Can't preview!
49-
</a>
50-
{{ end }}
51-
</td>
52-
<td style="text-align:center;">
53-
<a
54-
href="/ui/fs/del/{{ .ID }}"
55-
class="delete"
56-
onclick="return confirm('Are you sure you want to delete this file?\n\nThis is irreversible.')">
57-
</a>
58-
</td>
59-
</tr>
60-
{{end}}
61-
</tbody>
62-
</table>
63-
</div>
61+
</a>
62+
{{ end }}
63+
</td>
64+
<td style="text-align:center;">
65+
<a href="/ui/fs/del/{{ .ID }}" class="delete"
66+
onclick="return confirm('Are you sure you want to delete this file?\n\nThis is irreversible.')">
67+
</a>
68+
</td>
69+
</tr>
70+
{{end}}
71+
</tbody>
72+
</table>
73+
</div>
6474

6575
</body>
6676

67-
{{template "foot"}}
68-
77+
{{template "foot"}}

ui.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,15 @@ func (x *ui) fnDel(w http.ResponseWriter, r *http.Request) {
520520
}
521521

522522
func (x *ui) fsList(w http.ResponseWriter, r *http.Request) {
523-
conf, auth, err := middleware.Extract(r, false)
523+
conf, _, err := middleware.Extract(r, false)
524524
if err != nil {
525525
renderErr(w, r, err, x.log)
526526
return
527527
}
528528

529-
results, err := datastore.ListAllFiles(conf.Name, auth.AccountID)
529+
accountID := r.URL.Query().Get("id")
530+
531+
results, err := datastore.ListAllFiles(conf.Name, accountID)
530532
if err != nil {
531533
renderErr(w, r, err, x.log)
532534
return

0 commit comments

Comments
 (0)