Skip to content

Commit 14d6448

Browse files
authored
Change hotlink URL to be shorter (#253)
1 parent ab67370 commit 14d6448

File tree

8 files changed

+31
-7
lines changed

8 files changed

+31
-7
lines changed

internal/models/FileList.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func getHotlinkUrl(input FileApiOutput, serverUrl string, useFilename bool) stri
9999
return ""
100100
}
101101
if input.HotlinkId != "" {
102-
return serverUrl + "hotlink/" + input.HotlinkId
102+
return serverUrl + "h/" + input.HotlinkId
103103
}
104104
if useFilename {
105105
return serverUrl + "dh/" + input.Id + "/" + url.PathEscape(input.Name)

internal/models/FileList_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestGetHolinkUrl(t *testing.T) {
7575
test.IsEqualString(t, url, "")
7676
file.RequiresClientSideDecryption = false
7777
url = getHotlinkUrl(file, "testserver/", false)
78-
test.IsEqualString(t, url, "testserver/hotlink/test")
78+
test.IsEqualString(t, url, "testserver/h/test")
7979
file.HotlinkId = ""
8080
url = getHotlinkUrl(file, "testserver/", false)
8181
test.IsEqualString(t, url, "testserver/downloadFile?id=testfile")

internal/test/testconfiguration/TestConfiguration.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ func Create(initFiles bool) {
5858
writeTestSessions()
5959
writeTestFiles()
6060
database.SaveHotlink(models.File{Id: "n1tSTAGj8zan9KaT4u6p", HotlinkId: "PhSs6mFtf8O5YGlLMfNw9rYXx9XRNkzCnJZpQBi7inunv3Z4A.jpg", ExpireAt: time.Now().Add(time.Hour).Unix()})
61+
database.SaveHotlink(models.File{Id: "t4efyghvrill1w1zary2", HotlinkId: "wjqlzpq2.jpg", ExpireAt: time.Now().Add(time.Hour).Unix()})
6162
writeApiKeys()
6263
writeTestUploadStatus()
6364
database.Close()
6465

6566
if initFiles {
6667
os.MkdirAll(dataDir, 0777)
6768
os.WriteFile(dataDir+"/a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0", []byte("123"), 0777)
69+
os.WriteFile(dataDir+"/a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd1", []byte("123"), 0777)
6870
os.WriteFile(dataDir+"/c4f9375f9834b4e7f0a528cc65c055702bf5f24a", []byte("456"), 0777)
6971
os.WriteFile(dataDir+"/e017693e4a04a59d0b0f400fe98177fe7ee13cf7", []byte("789"), 0777)
7072
os.WriteFile(dataDir+"/2341354656543213246465465465432456898794", []byte("abc"), 0777)
@@ -370,6 +372,18 @@ func writeTestFiles() {
370372
HotlinkId: "PhSs6mFtf8O5YGlLMfNw9rYXx9XRNkzCnJZpQBi7inunv3Z4A.jpg",
371373
UserId: 5,
372374
})
375+
database.SaveMetaData(models.File{
376+
Id: "t4efyghvrill1w1zary2",
377+
Name: "picture.jpg",
378+
Size: "4 B",
379+
SHA1: "a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd1",
380+
ExpireAt: 2147483646,
381+
ExpireAtString: "2021-05-04 15:19",
382+
DownloadsRemaining: 1,
383+
ContentType: "text/html",
384+
HotlinkId: "wjqlzpq2.jpg",
385+
UserId: 5,
386+
})
373387
database.SaveMetaData(models.File{
374388
Id: "cleanuptest123456789",
375389
Name: "cleanup",

internal/webserver/Webserver.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func Start() {
102102
mux.HandleFunc("/error-header", showErrorHeader)
103103
mux.HandleFunc("/error-oauth", showErrorIntOAuth)
104104
mux.HandleFunc("/forgotpw", forgotPassword)
105-
mux.HandleFunc("/hotlink/", showHotlink)
105+
mux.HandleFunc("/h/", showHotlink)
106+
mux.HandleFunc("/hotlink/", showHotlink) // backward compatibility
106107
mux.HandleFunc("/index", showIndex)
107108
mux.HandleFunc("/login", showLogin)
108109
mux.HandleFunc("/logs", requireLogin(showLogs, true, false))
@@ -510,10 +511,11 @@ func showDownload(w http.ResponseWriter, r *http.Request) {
510511
helper.CheckIgnoreTimeout(err)
511512
}
512513

513-
// Handling of /hotlink/
514+
// Handling of /h/ and /hotlink/
514515
// Hotlinks an image or returns a static error image if image has expired
515516
func showHotlink(w http.ResponseWriter, r *http.Request) {
516517
hotlinkId := strings.Replace(r.URL.Path, "/hotlink/", "", 1)
518+
hotlinkId = strings.Replace(hotlinkId, "/h/", "", 1)
517519
addNoCacheHeader(w)
518520
file, ok := storage.GetFileByHotlink(hotlinkId)
519521
if !ok {

internal/webserver/Webserver_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,19 @@ func TestDownloadHotlink(t *testing.T) {
331331
Url: "http://127.0.0.1:53843/hotlink/PhSs6mFtf8O5YGlLMfNw9rYXx9XRNkzCnJZpQBi7inunv3Z4A.jpg",
332332
RequiredContent: []string{"123"},
333333
})
334+
test.HttpPageResult(t, test.HttpTestConfig{
335+
Url: "http://127.0.0.1:53843/h/wjqlzpq2.jpg",
336+
RequiredContent: []string{"123"},
337+
})
334338
// Download expired hotlink
335339
test.HttpPageResult(t, test.HttpTestConfig{
336340
Url: "http://127.0.0.1:53843/hotlink/PhSs6mFtf8O5YGlLMfNw9rYXx9XRNkzCnJZpQBi7inunv3Z4A.jpg",
337341
RequiredContent: []string{"The requested file has expired"},
338342
})
343+
test.HttpPageResult(t, test.HttpTestConfig{
344+
Url: "http://127.0.0.1:53843/h/wjqlzpq2.jpg",
345+
RequiredContent: []string{"The requested file has expired"},
346+
})
339347
}
340348

341349
func TestDownloadNoPassword(t *testing.T) {

internal/webserver/api/Api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ func TestList(t *testing.T) {
11891189
test.IsEqualInt(t, w.Code, 200)
11901190
err := json.Unmarshal(w.Body.Bytes(), &result)
11911191
test.IsNil(t, err)
1192-
test.IsEqualInt(t, len(result), 11)
1192+
test.IsEqualInt(t, len(result), 12)
11931193

11941194
removeUserPermission(t, idUser, models.UserPermListOtherUploads)
11951195
w, r = getRecorder(apiUrl, apiKey.Id, []test.Header{})

internal/webserver/web/static/apidocumentation/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@
10451045
"UrlHotlink": {
10461046
"type": "string",
10471047
"description": "The public hotlink URL for the file",
1048-
"example": "https://gokapi.server/hotlink/tDMs0U8MvRFwK69PfjagI7F87C13UVeQuOGDvtCG.jpg"
1048+
"example": "https://gokapi.server/h/tDMs0U8MvRFwK69PfjagI7F87C13UVeQuOGDvtCG.jpg"
10491049
},
10501050
"ExpireAt": {
10511051
"type": "integer",

openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@
10451045
"UrlHotlink": {
10461046
"type": "string",
10471047
"description": "The public hotlink URL for the file",
1048-
"example": "https://gokapi.server/hotlink/tDMs0U8MvRFwK69PfjagI7F87C13UVeQuOGDvtCG.jpg"
1048+
"example": "https://gokapi.server/h/tDMs0U8MvRFwK69PfjagI7F87C13UVeQuOGDvtCG.jpg"
10491049
},
10501050
"ExpireAt": {
10511051
"type": "integer",

0 commit comments

Comments
 (0)