Skip to content

Commit 1dc1199

Browse files
committed
Add sqlite support
fix http text preview
1 parent 38753d3 commit 1dc1199

File tree

13 files changed

+455
-486
lines changed

13 files changed

+455
-486
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ data/
2121
manager
2222
files
2323
.idea
24+
data.db

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ require (
2828
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2929
gopkg.in/yaml.v2 v2.4.0 // indirect
3030
gorm.io/driver/postgres v1.0.5
31+
gorm.io/driver/sqlite v1.1.4
3132
gorm.io/gorm v1.20.8
3233
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
151151
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
152152
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
153153
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
154+
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
154155
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
155156
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
156157
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
@@ -261,7 +262,10 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
261262
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
262263
gorm.io/driver/postgres v1.0.5 h1:raX6ezL/ciUmaYTvOq48jq1GE95aMC0CmxQYbxQ4Ufw=
263264
gorm.io/driver/postgres v1.0.5/go.mod h1:qrD92UurYzNctBMVCJ8C3VQEjffEuphycXtxOudXNCA=
265+
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
266+
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
264267
gorm.io/gorm v1.20.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
268+
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
265269
gorm.io/gorm v1.20.8 h1:iToaOdZgjNvlc44NFkxfLa3U9q63qwaxt0FdNCiwOMs=
266270
gorm.io/gorm v1.20.8/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
267271
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

handlers/Errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ var (
4040

4141
// RErrPermissionDenied if a user has no permission to run a certain command
4242
RErrPermissionDenied = NewRequestError("permission denied", http.StatusForbidden)
43+
44+
// RErrMissing if registration is not accepted
45+
RErrRegistrationNotAccepted = NewRequestError("Registration not accepted", http.StatusForbidden)
4346
)
4447

4548
// RequestError error appearing in a request

handlers/User.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func Login(handlerData web.HandlerData, w http.ResponseWriter, r *http.Request)
4747
// Register register handler
4848
func Register(handlerData web.HandlerData, w http.ResponseWriter, r *http.Request) error {
4949
if !handlerData.Config.Server.AllowRegistration {
50-
sendResponse(w, libdm.ResponseError, "Server doesn't accept registrations", nil, http.StatusForbidden)
51-
return nil
50+
return RErrRegistrationNotAccepted
5251
}
5352

5453
var request libdm.CredentialsRequest

handlers/web/PreviewHandler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func PrevievFileHandler(handlerData HandlerData, w http.ResponseWriter, r *http.
5252
return nil
5353
}
5454

55+
scheme := "http"
56+
if len(handlerData.Config.Webserver.SchemeOverwrite) > 0 {
57+
scheme = handlerData.Config.Webserver.SchemeOverwrite
58+
} else if r.TLS != nil {
59+
scheme = "https"
60+
}
61+
5562
templateData := models.PreviewTemplate{
5663
Filename: file.Name,
5764
PublicFilename: file.PublicFilename.String,
@@ -60,6 +67,7 @@ func PrevievFileHandler(handlerData HandlerData, w http.ResponseWriter, r *http.
6067
FileSizeStr: units.BinarySuffix(float64(file.FileSize)),
6168
Encrypted: (file.Encryption.Valid && libdm.EncryptionIValid(file.Encryption.Int32)),
6269
MimeType: file.FileType,
70+
Scheme: scheme,
6371
}
6472

6573
//Serve preview

html/index.html

Lines changed: 279 additions & 358 deletions
Large diffs are not rendered by default.

html/templates/Content.html

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
{{ define "content" }}
2-
{{ if IsVideoPreview .PreviewType }}
3-
<!-- Video Preview -->
4-
5-
<div class="center">
6-
<video controls>
7-
<source src='https://{{.Host}}/preview/raw/{{.PublicFilename}}' type="{{.MimeType}}">
8-
Your browser does not support HTML5 video.
9-
</video>
10-
</div>
11-
{{ end }}
12-
13-
{{ if IsImagePreview .PreviewType }}
14-
<!-- image Preview -->
15-
16-
<div class="center">
17-
<img src='https://{{.Host}}/preview/raw/{{.PublicFilename}}'>
18-
</div>
19-
{{ end }}
20-
21-
{{ if IsTextPreview .PreviewType }}
22-
<!-- Text preview -->
23-
24-
<center><h1>{{.Filename}}</h1></center>
25-
<textarea id="tdata" class="lined areapreview" style="width: 100%;min-height: 85vh;"></textarea>
26-
<script>
27-
$(function() {
28-
$(".lined").linedtextarea();
29-
});
30-
31-
//Encode html
32-
function htmlEncode(value){
33-
return $('<textarea/>').text(value).html();
34-
}
35-
36-
//Request data
37-
$.get("https://{{.Host}}/preview/raw/{{.PublicFilename}}",{}, function(data){
38-
document.getElementById("tdata").innerHTML = htmlEncode(data)
39-
})
40-
</script>
41-
{{ end }}
42-
43-
{{ if IsDefaultPreview .PreviewType }}
44-
<!-- Download View -->
45-
46-
<br>
47-
<span style="text-align: center;">
48-
<h1 style="color: black;font-size: 2.5em;">Can't view this shit. You have to press download</h1>
49-
</span>
50-
51-
<!-- Download button -->
52-
<div class="centered">
53-
<a href="https://{{.Host}}/preview/raw/{{.PublicFilename}}" class="downloadButton">Download</a>
54-
<br>
55-
<center>
56-
<span class="cv" style="font-size: 1.7rem;">({{.FileSizeStr}} {{ if .Encrypted}} encrypted{{ end }})</span>
57-
</center>
58-
</div>
59-
60-
<!-- GO -->
61-
<img style="bottom: 0;left: 20%;;position: absolute;" src="https://keepcoding.io/es/wp-content/uploads/sites/4/2017/04/go-lenguaje-programacion.png" width="300px">
62-
{{ end }}
63-
{{ end }}
2+
{{ if IsVideoPreview .PreviewType }}
3+
<!-- Video Preview -->
4+
5+
<div class="center">
6+
<video controls>
7+
<source src='{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}' type="{{.MimeType}}">
8+
Your browser does not support HTML5 video.
9+
</video>
10+
</div>
11+
{{ end }}
12+
13+
{{ if IsImagePreview .PreviewType }}
14+
<!-- image Preview -->
15+
16+
<div class="center">
17+
<img src='{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}'>
18+
</div>
19+
{{ end }}
20+
21+
{{ if IsTextPreview .PreviewType }}
22+
<!-- Text preview -->
23+
24+
<center>
25+
<h1>{{.Filename}}</h1>
26+
</center>
27+
<textarea id="tdata" class="lined areapreview" style="width: 100%;min-height: 85vh;"></textarea>
28+
<script>
29+
$(function () {
30+
$(".lined").linedtextarea();
31+
});
32+
33+
//Encode html
34+
function htmlEncode(value) {
35+
return $('<textarea/>').text(value).html();
36+
}
37+
38+
//Request data
39+
$.get("{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}", {}, function (data) {
40+
document.getElementById("tdata").innerHTML = htmlEncode(data)
41+
})
42+
</script>
43+
{{ end }}
44+
45+
{{ if IsDefaultPreview .PreviewType }}
46+
<!-- Download View -->
47+
48+
<br>
49+
<span style="text-align: center;">
50+
<h1 style="color: black;font-size: 2.5em;">Can't view this shit. You have to press download</h1>
51+
</span>
52+
53+
<!-- Download button -->
54+
<div class="centered">
55+
<a href="{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}" class="downloadButton">Download</a>
56+
<br>
57+
<center>
58+
<span class="cv" style="font-size: 1.7rem;">({{.FileSizeStr}} {{ if .Encrypted}} encrypted{{ end }})</span>
59+
</center>
60+
</div>
61+
{{ end }}
62+
{{ end }}

html/templates/Preview.html

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,70 @@
11
<!DOCTYPE html>
22
<html lang="en" style="height: 100%;">
3-
<head>
4-
<meta property="og:image" content="https://{{.Host}}/preview/raw/{{.PublicFilename}}">
5-
<meta property="og:url" content="https://{{.Host}}/preview/raw/{{.PublicFilename}}" />
6-
<meta property="og:type" content="website" />
7-
<meta property="og:site_name" content="A very illegal upload service">
8-
<meta property="og:title" content="Not an illegal file" />
9-
<style>
10-
.centered {
11-
position: absolute;
12-
top: 50%;
13-
left: 50%;
14-
-ms-transform: translate(-50%, -50%);
15-
transform: translate(-50%, -50%);
16-
max-width: 100%;
17-
max-height: 100%;
18-
}
193

20-
.center {
21-
position: absolute;
22-
top: 50%;
23-
left: 50%;
24-
-ms-transform: translate(-50%, -50%);
25-
transform: translate(-50%, -50%);
26-
max-width: 100%;
27-
max-height: 100%;
28-
}
4+
<head>
5+
<meta property="og:image" content="{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}">
6+
<meta property="og:url" content="{{.Scheme}}://{{.Host}}/preview/raw/{{.PublicFilename}}" />
7+
<meta property="og:type" content="website" />
8+
<meta property="og:site_name" content="A very illegal upload service">
9+
<meta property="og:title" content="Not an illegal file" />
10+
<style>
11+
.centered {
12+
position: absolute;
13+
top: 50%;
14+
left: 50%;
15+
-ms-transform: translate(-50%, -50%);
16+
transform: translate(-50%, -50%);
17+
max-width: 100%;
18+
max-height: 100%;
19+
}
2920

30-
.downloadButton {
31-
background:linear-gradient(to bottom, #f9f9f9 5%, #e9e9e9 100%);
32-
background-color:#f9f9f9;
33-
border-radius:10px;
34-
border:1px solid #dcdcdc;
35-
display:inline-block;
36-
cursor:pointer;
37-
color:#707070;
38-
font-family:Times New Roman;
39-
font-size:2.5rem;
40-
font-weight:bold;
41-
padding:14px 55px;
42-
text-decoration:none;
43-
}
44-
.downloadButton:active {
45-
position:relative;
46-
background-color:#dfdfdf;
47-
background:#dfdfdf;
48-
top:1px;
49-
}
21+
.center {
22+
position: absolute;
23+
top: 50%;
24+
left: 50%;
25+
-ms-transform: translate(-50%, -50%);
26+
transform: translate(-50%, -50%);
27+
max-width: 100%;
28+
max-height: 100%;
29+
}
5030

51-
.cv {
52-
line-height: 60px;
53-
text-align: center;
54-
}
55-
</style>
56-
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
57-
<link href="/static/preview/linenumbers.css" type="text/css" rel="stylesheet" />
58-
<script src="/static/preview/linenumbers.js"></script>
31+
.downloadButton {
32+
background: linear-gradient(to bottom, #f9f9f9 5%, #e9e9e9 100%);
33+
background-color: #f9f9f9;
34+
border-radius: 10px;
35+
border: 1px solid #dcdcdc;
36+
display: inline-block;
37+
cursor: pointer;
38+
color: #707070;
39+
font-family: Times New Roman;
40+
font-size: 2.5rem;
41+
font-weight: bold;
42+
padding: 14px 55px;
43+
text-decoration: none;
44+
}
5945

60-
</head>
61-
<body background="https://images.pexels.com/photos/1242348/pexels-photo-1242348.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" style="background-size: 300% auto;height: 100%;margin: 0;">
62-
{{template "content" .}}
63-
</body>
64-
</html>
46+
.downloadButton:active {
47+
position: relative;
48+
background-color: #dfdfdf;
49+
background: #dfdfdf;
50+
top: 1px;
51+
}
52+
53+
.cv {
54+
line-height: 60px;
55+
text-align: center;
56+
}
57+
</style>
58+
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
59+
<link href="/static/preview/linenumbers.css" type="text/css" rel="stylesheet" />
60+
<script src="/static/preview/linenumbers.js"></script>
61+
62+
</head>
63+
64+
<body
65+
background="https://images.pexels.com/photos/1242348/pexels-photo-1242348.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
66+
style="background-size: 300% auto;height: 100%;margin: 0;">
67+
{{template "content" .}}
68+
</body>
69+
70+
</html>

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func main() {
121121
}
122122

123123
// Check if connected to db
124-
if isConnected, err := storage.CheckConnection(db); !isConnected {
124+
if isConnected, err := storage.CheckConnection(db, config); !isConnected {
125125
log.Fatalln(err)
126126
return
127127
}

0 commit comments

Comments
 (0)