2026-05-25 15:36:49 +03:00
package handlers
import (
"net/http"
2026-05-31 02:14:10 +03:00
"strconv"
2026-05-25 15:36:49 +03:00
2026-05-30 15:42:35 +03:00
"warpbox.dev/backend/libs/services"
2026-05-25 15:36:49 +03:00
"warpbox.dev/backend/libs/web"
)
type homeData struct {
MaxUploadSize string
2026-05-30 17:23:20 +03:00
LimitSummary string
2026-05-30 15:42:35 +03:00
Collections [ ] collectionView
IsAdmin bool
2026-05-30 17:23:20 +03:00
AnonymousOpen bool
2026-05-25 15:36:49 +03:00
}
func ( a * App ) Home ( w http . ResponseWriter , r * http . Request ) {
2026-05-30 15:42:35 +03:00
currentUser := a . currentPublicUser ( r )
var collections [ ] collectionView
var isAdmin bool
2026-05-30 17:23:20 +03:00
var user services . User
var loggedIn bool
if current , ok := a . currentUser ( r ) ; ok {
user = current
loggedIn = true
2026-05-30 15:42:35 +03:00
isAdmin = user . Role == services . UserRoleAdmin
userCollections , err := a . authService . ListCollections ( user . ID )
if err == nil {
collections = make ( [ ] collectionView , 0 , len ( userCollections ) )
for _ , collection := range userCollections {
collections = append ( collections , collectionView { ID : collection . ID , Name : collection . Name } )
}
}
}
2026-05-30 17:23:20 +03:00
settings , err := a . settingsService . UploadPolicy ( )
if err != nil {
http . Error ( w , "unable to load upload policy" , http . StatusInternalServerError )
return
}
maxUploadSize , limitSummary := a . homeUploadPolicyLabels ( settings , user , loggedIn , isAdmin )
a . renderPage ( w , r , http . StatusOK , "home.html" , web . PageData {
2026-05-25 15:36:49 +03:00
Title : "Upload your files" ,
Description : "Upload and share files through a self-hosted Warpbox instance." ,
2026-05-30 15:42:35 +03:00
CurrentUser : currentUser ,
2026-05-25 15:36:49 +03:00
Data : homeData {
2026-05-30 17:23:20 +03:00
MaxUploadSize : maxUploadSize ,
LimitSummary : limitSummary ,
2026-05-30 15:42:35 +03:00
Collections : collections ,
IsAdmin : isAdmin ,
2026-05-30 17:23:20 +03:00
AnonymousOpen : settings . AnonymousUploadsEnabled ,
2026-05-25 15:36:49 +03:00
} ,
} )
}
2026-05-30 17:23:20 +03:00
func ( a * App ) homeUploadPolicyLabels ( settings services . UploadPolicySettings , user services . User , loggedIn , isAdmin bool ) ( string , string ) {
if isAdmin {
return "No file size limit" , "Admin uploads bypass storage and daily caps."
}
if ! loggedIn {
if ! settings . AnonymousUploadsEnabled {
return "Anonymous uploads disabled" , "Sign in to upload files."
}
2026-05-31 02:14:10 +03:00
return services . FormatMegabytesLabel ( settings . AnonymousMaxUploadMB ) , "Daily anonymous cap: " + services . FormatMegabytesLabel ( settings . AnonymousDailyUploadMB ) + " per IP · " + strconv . Itoa ( settings . AnonymousMaxDays ) + " day max."
2026-05-30 17:23:20 +03:00
}
2026-05-31 02:14:10 +03:00
policy := a . settingsService . EffectivePolicyForUser ( settings , user )
maxUpload := a . uploadService . MaxUploadSizeLabel ( )
2026-05-31 14:01:38 +03:00
if policy . MaxUploadMB < 0 {
maxUpload = "unlimited"
} else if policy . MaxUploadMB > 0 {
2026-05-31 02:14:10 +03:00
maxUpload = services . FormatMegabytesLabel ( policy . MaxUploadMB )
2026-05-30 17:23:20 +03:00
}
2026-05-31 02:14:10 +03:00
quota := "unlimited"
if policy . StorageQuotaSet {
quota = services . FormatMegabytesLabel ( policy . StorageQuotaMB )
}
return maxUpload , "Daily cap: " + services . FormatMegabytesLabel ( policy . DailyUploadMB ) + " · Storage quota: " + quota + " · " + strconv . Itoa ( policy . MaxDays ) + " day max."
2026-05-30 17:23:20 +03:00
}