2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-10-09 12:17:28 +00:00
|
|
|
"bytes"
|
2021-05-30 21:07:46 +00:00
|
|
|
"encoding/json"
|
2021-11-13 12:25:43 +00:00
|
|
|
"errors"
|
2021-05-06 19:35:43 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2021-10-09 12:17:28 +00:00
|
|
|
"io"
|
2023-06-16 19:06:21 +00:00
|
|
|
"math"
|
2021-05-06 19:35:43 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-11-06 13:13:20 +00:00
|
|
|
"strconv"
|
2021-10-09 12:17:28 +00:00
|
|
|
"strings"
|
2021-05-06 19:35:43 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-27 07:40:46 +00:00
|
|
|
"github.com/go-chi/render"
|
2021-05-06 19:35:43 +00:00
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
2022-09-19 17:58:35 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/mfa"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/smtp"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-04 10:11:04 +00:00
|
|
|
templateClientDir = "webclient"
|
|
|
|
templateClientBase = "base.html"
|
|
|
|
templateClientBaseLogin = "baselogin.html"
|
|
|
|
templateClientLogin = "login.html"
|
|
|
|
templateClientFiles = "files.html"
|
|
|
|
templateClientMessage = "message.html"
|
2021-09-29 16:46:15 +00:00
|
|
|
templateClientProfile = "profile.html"
|
|
|
|
templateClientChangePwd = "changepassword.html"
|
2021-09-04 10:11:04 +00:00
|
|
|
templateClientTwoFactor = "twofactor.html"
|
|
|
|
templateClientTwoFactorRecovery = "twofactor-recovery.html"
|
|
|
|
templateClientMFA = "mfa.html"
|
2021-10-09 12:17:28 +00:00
|
|
|
templateClientEditFile = "editfile.html"
|
2021-11-06 13:13:20 +00:00
|
|
|
templateClientShare = "share.html"
|
|
|
|
templateClientShares = "shares.html"
|
2021-11-25 18:24:32 +00:00
|
|
|
templateClientViewPDF = "viewpdf.html"
|
2023-02-12 07:29:53 +00:00
|
|
|
templateShareLogin = "sharelogin.html"
|
2022-02-15 18:19:25 +00:00
|
|
|
templateUploadToShare = "shareupload.html"
|
2023-11-04 09:54:52 +00:00
|
|
|
pageClientFilesTitle = "Files"
|
2021-11-06 13:13:20 +00:00
|
|
|
pageClientSharesTitle = "Shares"
|
2021-09-29 16:46:15 +00:00
|
|
|
pageClientProfileTitle = "My Profile"
|
|
|
|
pageClientChangePwdTitle = "Change password"
|
|
|
|
pageClient2FATitle = "Two-factor auth"
|
2021-10-09 12:17:28 +00:00
|
|
|
pageClientEditFileTitle = "Edit file"
|
2021-11-13 12:25:43 +00:00
|
|
|
pageClientForgotPwdTitle = "SFTPGo WebClient - Forgot password"
|
|
|
|
pageClientResetPwdTitle = "SFTPGo WebClient - Reset password"
|
2022-02-06 15:46:43 +00:00
|
|
|
pageExtShareTitle = "Shared files"
|
2022-02-15 18:19:25 +00:00
|
|
|
pageUploadToShareTitle = "Upload to share"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// condResult is the result of an HTTP request precondition check.
|
|
|
|
// See https://tools.ietf.org/html/rfc7232 section 3.
|
|
|
|
type condResult int
|
|
|
|
|
|
|
|
const (
|
|
|
|
condNone condResult = iota
|
|
|
|
condTrue
|
|
|
|
condFalse
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
clientTemplates = make(map[string]*template.Template)
|
|
|
|
unixEpochTime = time.Unix(0, 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
// isZeroTime reports whether t is obviously unspecified (either zero or Unix()=0).
|
|
|
|
func isZeroTime(t time.Time) bool {
|
|
|
|
return t.IsZero() || t.Equal(unixEpochTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseClientPage struct {
|
2022-08-01 17:41:18 +00:00
|
|
|
Title string
|
|
|
|
CurrentURL string
|
|
|
|
FilesURL string
|
|
|
|
SharesURL string
|
|
|
|
ShareURL string
|
|
|
|
ProfileURL string
|
|
|
|
ChangePwdURL string
|
|
|
|
StaticURL string
|
|
|
|
LogoutURL string
|
2023-11-04 09:54:52 +00:00
|
|
|
LoginURL string
|
|
|
|
EditURL string
|
2022-08-01 17:41:18 +00:00
|
|
|
MFAURL string
|
|
|
|
MFATitle string
|
|
|
|
FilesTitle string
|
|
|
|
SharesTitle string
|
|
|
|
ProfileTitle string
|
|
|
|
Version string
|
|
|
|
CSRFToken string
|
|
|
|
LoggedUser *dataprovider.User
|
|
|
|
Branding UIBranding
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirMapping struct {
|
|
|
|
DirName string
|
|
|
|
Href string
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:24:32 +00:00
|
|
|
type viewPDFPage struct {
|
|
|
|
Title string
|
|
|
|
URL string
|
|
|
|
StaticURL string
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding UIBranding
|
2021-11-25 18:24:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:17:28 +00:00
|
|
|
type editFilePage struct {
|
|
|
|
baseClientPage
|
|
|
|
CurrentDir string
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL string
|
2021-10-09 12:17:28 +00:00
|
|
|
Path string
|
|
|
|
Name string
|
2021-11-25 18:24:32 +00:00
|
|
|
ReadOnly bool
|
2021-10-09 12:17:28 +00:00
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type filesPage struct {
|
|
|
|
baseClientPage
|
2023-11-04 09:54:52 +00:00
|
|
|
CurrentDir string
|
|
|
|
DirsURL string
|
|
|
|
FileActionsURL string
|
|
|
|
DownloadURL string
|
|
|
|
ViewPDFURL string
|
|
|
|
FileURL string
|
|
|
|
CanAddFiles bool
|
|
|
|
CanCreateDirs bool
|
|
|
|
CanRename bool
|
|
|
|
CanDelete bool
|
|
|
|
CanDownload bool
|
|
|
|
CanShare bool
|
|
|
|
ShareUploadBaseURL string
|
|
|
|
Error string
|
|
|
|
Paths []dirMapping
|
|
|
|
HasIntegrations bool
|
|
|
|
QuotaUsage *userQuotaUsage
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 07:29:53 +00:00
|
|
|
type shareLoginPage struct {
|
|
|
|
CurrentURL string
|
|
|
|
Version string
|
|
|
|
Error string
|
|
|
|
CSRFToken string
|
|
|
|
StaticURL string
|
|
|
|
Branding UIBranding
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:19:25 +00:00
|
|
|
type shareUploadPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Share *dataprovider.Share
|
|
|
|
UploadBasePath string
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
type clientMessagePage struct {
|
|
|
|
baseClientPage
|
|
|
|
Error string
|
|
|
|
Success string
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
type clientProfilePage struct {
|
2021-05-06 19:35:43 +00:00
|
|
|
baseClientPage
|
2021-08-17 16:08:32 +00:00
|
|
|
PublicKeys []string
|
2021-09-29 16:46:15 +00:00
|
|
|
CanSubmit bool
|
2021-08-17 16:08:32 +00:00
|
|
|
AllowAPIKeyAuth bool
|
2021-09-29 16:46:15 +00:00
|
|
|
Email string
|
|
|
|
Description string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
type changeClientPasswordPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Error string
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
type clientMFAPage struct {
|
|
|
|
baseClientPage
|
|
|
|
TOTPConfigs []string
|
2022-01-06 09:11:47 +00:00
|
|
|
TOTPConfig dataprovider.UserTOTPConfig
|
2021-09-04 10:11:04 +00:00
|
|
|
GenerateTOTPURL string
|
|
|
|
ValidateTOTPURL string
|
|
|
|
SaveTOTPURL string
|
|
|
|
RecCodesURL string
|
|
|
|
Protocols []string
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
type clientSharesPage struct {
|
|
|
|
baseClientPage
|
|
|
|
Shares []dataprovider.Share
|
|
|
|
BasePublicSharesURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientSharePage struct {
|
|
|
|
baseClientPage
|
|
|
|
Share *dataprovider.Share
|
|
|
|
Error string
|
|
|
|
IsAdd bool
|
|
|
|
}
|
|
|
|
|
2023-06-16 19:06:21 +00:00
|
|
|
type userQuotaUsage struct {
|
|
|
|
QuotaSize int64
|
|
|
|
QuotaFiles int
|
|
|
|
UsedQuotaSize int64
|
|
|
|
UsedQuotaFiles int
|
|
|
|
UploadDataTransfer int64
|
|
|
|
DownloadDataTransfer int64
|
|
|
|
TotalDataTransfer int64
|
|
|
|
UsedUploadDataTransfer int64
|
|
|
|
UsedDownloadDataTransfer int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) HasQuotaInfo() bool {
|
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if u.HasDiskQuota() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return u.HasTranferQuota()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) HasDiskQuota() bool {
|
|
|
|
if u.QuotaSize > 0 || u.UsedQuotaSize > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return u.QuotaFiles > 0 || u.UsedQuotaFiles > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) HasTranferQuota() bool {
|
|
|
|
if u.TotalDataTransfer > 0 || u.UploadDataTransfer > 0 || u.DownloadDataTransfer > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return u.UsedDownloadDataTransfer > 0 || u.UsedUploadDataTransfer > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetQuotaSize() string {
|
|
|
|
if u.QuotaSize > 0 {
|
|
|
|
return fmt.Sprintf("%s/%s", util.ByteCountIEC(u.UsedQuotaSize), util.ByteCountIEC(u.QuotaSize))
|
|
|
|
}
|
|
|
|
if u.UsedQuotaSize > 0 {
|
|
|
|
return util.ByteCountIEC(u.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetQuotaFiles() string {
|
|
|
|
if u.QuotaFiles > 0 {
|
|
|
|
return fmt.Sprintf("%d/%d", u.UsedQuotaFiles, u.QuotaFiles)
|
|
|
|
}
|
|
|
|
if u.UsedQuotaFiles > 0 {
|
|
|
|
return strconv.FormatInt(int64(u.UsedQuotaFiles), 10)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetQuotaSizePercentage() int {
|
|
|
|
if u.QuotaSize > 0 {
|
|
|
|
return int(math.Round(100 * float64(u.UsedQuotaSize) / float64(u.QuotaSize)))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetQuotaFilesPercentage() int {
|
|
|
|
if u.QuotaFiles > 0 {
|
|
|
|
return int(math.Round(100 * float64(u.UsedQuotaFiles) / float64(u.QuotaFiles)))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsQuotaSizeLow() bool {
|
|
|
|
return u.GetQuotaSizePercentage() > 85
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsQuotaFilesLow() bool {
|
|
|
|
return u.GetQuotaFilesPercentage() > 85
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsDiskQuotaLow() bool {
|
|
|
|
return u.IsQuotaSizeLow() || u.IsQuotaFilesLow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetTotalTransferQuota() string {
|
|
|
|
total := u.UsedUploadDataTransfer + u.UsedDownloadDataTransfer
|
|
|
|
if u.TotalDataTransfer > 0 {
|
|
|
|
return fmt.Sprintf("%s/%s", util.ByteCountIEC(total), util.ByteCountIEC(u.TotalDataTransfer*1048576))
|
|
|
|
}
|
|
|
|
if total > 0 {
|
|
|
|
return util.ByteCountIEC(total)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetUploadTransferQuota() string {
|
|
|
|
if u.UploadDataTransfer > 0 {
|
|
|
|
return fmt.Sprintf("%s/%s", util.ByteCountIEC(u.UsedUploadDataTransfer),
|
|
|
|
util.ByteCountIEC(u.UploadDataTransfer*1048576))
|
|
|
|
}
|
|
|
|
if u.UsedUploadDataTransfer > 0 {
|
|
|
|
return util.ByteCountIEC(u.UsedUploadDataTransfer)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetDownloadTransferQuota() string {
|
|
|
|
if u.DownloadDataTransfer > 0 {
|
|
|
|
return fmt.Sprintf("%s/%s", util.ByteCountIEC(u.UsedDownloadDataTransfer),
|
|
|
|
util.ByteCountIEC(u.DownloadDataTransfer*1048576))
|
|
|
|
}
|
|
|
|
if u.UsedDownloadDataTransfer > 0 {
|
|
|
|
return util.ByteCountIEC(u.UsedDownloadDataTransfer)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetTotalTransferQuotaPercentage() int {
|
|
|
|
if u.TotalDataTransfer > 0 {
|
|
|
|
return int(math.Round(100 * float64(u.UsedDownloadDataTransfer+u.UsedUploadDataTransfer) / float64(u.TotalDataTransfer*1048576)))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetUploadTransferQuotaPercentage() int {
|
|
|
|
if u.UploadDataTransfer > 0 {
|
|
|
|
return int(math.Round(100 * float64(u.UsedUploadDataTransfer) / float64(u.UploadDataTransfer*1048576)))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) GetDownloadTransferQuotaPercentage() int {
|
|
|
|
if u.DownloadDataTransfer > 0 {
|
|
|
|
return int(math.Round(100 * float64(u.UsedDownloadDataTransfer) / float64(u.DownloadDataTransfer*1048576)))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsTotalTransferQuotaLow() bool {
|
|
|
|
if u.TotalDataTransfer > 0 {
|
|
|
|
return u.GetTotalTransferQuotaPercentage() > 85
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsUploadTransferQuotaLow() bool {
|
|
|
|
if u.UploadDataTransfer > 0 {
|
|
|
|
return u.GetUploadTransferQuotaPercentage() > 85
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsDownloadTransferQuotaLow() bool {
|
|
|
|
if u.DownloadDataTransfer > 0 {
|
|
|
|
return u.GetDownloadTransferQuotaPercentage() > 85
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsTransferQuotaLow() bool {
|
|
|
|
return u.IsTotalTransferQuotaLow() || u.IsUploadTransferQuotaLow() || u.IsDownloadTransferQuotaLow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userQuotaUsage) IsQuotaLow() bool {
|
|
|
|
return u.IsDiskQuotaLow() || u.IsTransferQuotaLow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserQuotaUsage(u *dataprovider.User) *userQuotaUsage {
|
|
|
|
return &userQuotaUsage{
|
|
|
|
QuotaSize: u.QuotaSize,
|
|
|
|
QuotaFiles: u.QuotaFiles,
|
|
|
|
UsedQuotaSize: u.UsedQuotaSize,
|
|
|
|
UsedQuotaFiles: u.UsedQuotaFiles,
|
|
|
|
TotalDataTransfer: u.TotalDataTransfer,
|
|
|
|
UploadDataTransfer: u.UploadDataTransfer,
|
|
|
|
DownloadDataTransfer: u.DownloadDataTransfer,
|
|
|
|
UsedUploadDataTransfer: u.UsedUploadDataTransfer,
|
|
|
|
UsedDownloadDataTransfer: u.UsedDownloadDataTransfer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
func getFileObjectURL(baseDir, name, baseWebPath string) string {
|
|
|
|
return fmt.Sprintf("%v?path=%v&_=%v", baseWebPath, url.QueryEscape(path.Join(baseDir, name)), time.Now().UTC().Unix())
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFileObjectModTime(t time.Time) string {
|
|
|
|
if isZeroTime(t) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return t.Format("2006-01-02 15:04")
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadClientTemplates(templatesPath string) {
|
|
|
|
filesPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientFiles),
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
editFilePath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-10-09 12:17:28 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientEditFile),
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-11-06 13:13:20 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientShares),
|
|
|
|
}
|
|
|
|
sharePaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-11-06 13:13:20 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientShare),
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
profilePaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-09-29 16:46:15 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientProfile),
|
|
|
|
}
|
|
|
|
changePwdPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
2021-09-29 16:46:15 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientChangePwd),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
loginPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientLogin),
|
|
|
|
}
|
|
|
|
messagePath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-05-06 19:35:43 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientMessage),
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientMFA),
|
|
|
|
}
|
|
|
|
twoFactorPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientTwoFactor),
|
|
|
|
}
|
|
|
|
twoFactorRecoveryPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-09-04 10:11:04 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientTwoFactorRecovery),
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
forgotPwdPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateForgotPassword),
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
resetPwdPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateResetPassword),
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
2021-11-25 18:24:32 +00:00
|
|
|
viewPDFPaths := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2021-11-25 18:24:32 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientViewPDF),
|
|
|
|
}
|
2023-02-12 07:29:53 +00:00
|
|
|
shareLoginPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2023-02-12 07:29:53 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBaseLogin),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateShareLogin),
|
|
|
|
}
|
2022-02-15 18:19:25 +00:00
|
|
|
shareUploadPath := []string{
|
2023-11-04 09:54:52 +00:00
|
|
|
filepath.Join(templatesPath, templateCommonDir, templateCommonBase),
|
2022-02-15 18:19:25 +00:00
|
|
|
filepath.Join(templatesPath, templateClientDir, templateClientBase),
|
|
|
|
filepath.Join(templatesPath, templateClientDir, templateUploadToShare),
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
filesTmpl := util.LoadTemplate(nil, filesPaths...)
|
2021-09-29 16:46:15 +00:00
|
|
|
profileTmpl := util.LoadTemplate(nil, profilePaths...)
|
|
|
|
changePwdTmpl := util.LoadTemplate(nil, changePwdPaths...)
|
2021-07-11 13:26:51 +00:00
|
|
|
loginTmpl := util.LoadTemplate(nil, loginPath...)
|
|
|
|
messageTmpl := util.LoadTemplate(nil, messagePath...)
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaTmpl := util.LoadTemplate(nil, mfaPath...)
|
|
|
|
twoFactorTmpl := util.LoadTemplate(nil, twoFactorPath...)
|
|
|
|
twoFactorRecoveryTmpl := util.LoadTemplate(nil, twoFactorRecoveryPath...)
|
2021-10-09 12:17:28 +00:00
|
|
|
editFileTmpl := util.LoadTemplate(nil, editFilePath...)
|
2023-02-12 07:29:53 +00:00
|
|
|
shareLoginTmpl := util.LoadTemplate(nil, shareLoginPath...)
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesTmpl := util.LoadTemplate(nil, sharesPaths...)
|
|
|
|
shareTmpl := util.LoadTemplate(nil, sharePaths...)
|
2021-11-13 12:25:43 +00:00
|
|
|
forgotPwdTmpl := util.LoadTemplate(nil, forgotPwdPaths...)
|
|
|
|
resetPwdTmpl := util.LoadTemplate(nil, resetPwdPaths...)
|
2021-11-25 18:24:32 +00:00
|
|
|
viewPDFTmpl := util.LoadTemplate(nil, viewPDFPaths...)
|
2022-02-15 18:19:25 +00:00
|
|
|
shareUploadTmpl := util.LoadTemplate(nil, shareUploadPath...)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
clientTemplates[templateClientFiles] = filesTmpl
|
2021-09-29 16:46:15 +00:00
|
|
|
clientTemplates[templateClientProfile] = profileTmpl
|
|
|
|
clientTemplates[templateClientChangePwd] = changePwdTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
clientTemplates[templateClientLogin] = loginTmpl
|
|
|
|
clientTemplates[templateClientMessage] = messageTmpl
|
2021-09-04 10:11:04 +00:00
|
|
|
clientTemplates[templateClientMFA] = mfaTmpl
|
|
|
|
clientTemplates[templateClientTwoFactor] = twoFactorTmpl
|
|
|
|
clientTemplates[templateClientTwoFactorRecovery] = twoFactorRecoveryTmpl
|
2021-10-09 12:17:28 +00:00
|
|
|
clientTemplates[templateClientEditFile] = editFileTmpl
|
2021-11-06 13:13:20 +00:00
|
|
|
clientTemplates[templateClientShares] = sharesTmpl
|
|
|
|
clientTemplates[templateClientShare] = shareTmpl
|
2021-11-13 12:25:43 +00:00
|
|
|
clientTemplates[templateForgotPassword] = forgotPwdTmpl
|
|
|
|
clientTemplates[templateResetPassword] = resetPwdTmpl
|
2021-11-25 18:24:32 +00:00
|
|
|
clientTemplates[templateClientViewPDF] = viewPDFTmpl
|
2023-02-12 07:29:53 +00:00
|
|
|
clientTemplates[templateShareLogin] = shareLoginTmpl
|
2022-02-15 18:19:25 +00:00
|
|
|
clientTemplates[templateUploadToShare] = shareUploadTmpl
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) getBaseClientPageData(title, currentURL string, r *http.Request) baseClientPage {
|
2021-05-06 19:35:43 +00:00
|
|
|
var csrfToken string
|
|
|
|
if currentURL != "" {
|
2022-03-26 07:41:50 +00:00
|
|
|
csrfToken = createCSRFToken(util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
v := version.Get()
|
|
|
|
|
2023-11-04 09:54:52 +00:00
|
|
|
data := baseClientPage{
|
2022-08-01 17:41:18 +00:00
|
|
|
Title: title,
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
FilesURL: webClientFilesPath,
|
|
|
|
SharesURL: webClientSharesPath,
|
|
|
|
ShareURL: webClientSharePath,
|
|
|
|
ProfileURL: webClientProfilePath,
|
|
|
|
ChangePwdURL: webChangeClientPwdPath,
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
LogoutURL: webClientLogoutPath,
|
2023-11-04 09:54:52 +00:00
|
|
|
EditURL: webClientEditFilePath,
|
2022-08-01 17:41:18 +00:00
|
|
|
MFAURL: webClientMFAPath,
|
|
|
|
MFATitle: pageClient2FATitle,
|
|
|
|
FilesTitle: pageClientFilesTitle,
|
|
|
|
SharesTitle: pageClientSharesTitle,
|
|
|
|
ProfileTitle: pageClientProfileTitle,
|
|
|
|
Version: fmt.Sprintf("%v-%v", v.Version, v.CommitHash),
|
|
|
|
CSRFToken: csrfToken,
|
|
|
|
LoggedUser: getUserFromToken(r),
|
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
if !strings.HasPrefix(r.RequestURI, webClientPubSharesPath) {
|
|
|
|
data.LoginURL = webClientLoginPath
|
|
|
|
}
|
|
|
|
return data
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
func (s *httpdServer) renderClientForgotPwdPage(w http.ResponseWriter, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := forgotPwdPage{
|
|
|
|
CurrentURL: webClientForgotPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
2023-11-04 09:54:52 +00:00
|
|
|
LoginURL: webClientLoginPath,
|
2021-11-13 12:25:43 +00:00
|
|
|
Title: pageClientForgotPwdTitle,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateForgotPassword, data)
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:14:47 +00:00
|
|
|
func (s *httpdServer) renderClientResetPwdPage(w http.ResponseWriter, _ *http.Request, error, ip string) {
|
2021-11-13 12:25:43 +00:00
|
|
|
data := resetPwdPage{
|
|
|
|
CurrentURL: webClientResetPwdPath,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-11-13 12:25:43 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
2023-11-04 09:54:52 +00:00
|
|
|
LoginURL: webClientLoginPath,
|
2021-11-13 12:25:43 +00:00
|
|
|
Title: pageClientResetPwdTitle,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateResetPassword, data)
|
|
|
|
}
|
|
|
|
|
2023-02-12 07:29:53 +00:00
|
|
|
func (s *httpdServer) renderShareLoginPage(w http.ResponseWriter, currentURL, error, ip string) {
|
|
|
|
data := shareLoginPage{
|
|
|
|
CurrentURL: currentURL,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
|
|
|
CSRFToken: createCSRFToken(ip),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Branding: s.binding.Branding.WebClient,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateShareLogin, data)
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
func renderClientTemplate(w http.ResponseWriter, tmplName string, data any) {
|
2021-05-06 19:35:43 +00:00
|
|
|
err := clientTemplates[tmplName].ExecuteTemplate(w, tmplName, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientMessagePage(w http.ResponseWriter, r *http.Request, title, body string, statusCode int, err error, message string) {
|
2022-09-19 17:58:35 +00:00
|
|
|
var errorString strings.Builder
|
2021-05-06 19:35:43 +00:00
|
|
|
if body != "" {
|
2022-09-19 17:58:35 +00:00
|
|
|
errorString.WriteString(body)
|
|
|
|
errorString.WriteString(" ")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-19 17:58:35 +00:00
|
|
|
errorString.WriteString(err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
data := clientMessagePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(title, "", r),
|
2022-09-19 17:58:35 +00:00
|
|
|
Error: errorString.String(),
|
2021-05-06 19:35:43 +00:00
|
|
|
Success: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
renderClientTemplate(w, templateClientMessage, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientInternalServerErrorPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(w, r, page500Title, page500Body, http.StatusInternalServerError, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientBadRequestPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(w, r, page400Title, "", http.StatusBadRequest, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientForbiddenPage(w http.ResponseWriter, r *http.Request, body string) {
|
|
|
|
s.renderClientMessagePage(w, r, page403Title, "", http.StatusForbidden, nil, body)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientNotFoundPage(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
s.renderClientMessagePage(w, r, page404Title, page404Body, http.StatusNotFound, err, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 16:14:47 +00:00
|
|
|
func (s *httpdServer) renderClientTwoFactorPage(w http.ResponseWriter, r *http.Request, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-09-04 10:11:04 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
RecoveryURL: webClientTwoFactorRecoveryPath,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2023-06-08 16:14:47 +00:00
|
|
|
if next := r.URL.Query().Get("next"); strings.HasPrefix(next, webClientFilesPath) {
|
|
|
|
data.CurrentURL += "?next=" + url.QueryEscape(next)
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
renderClientTemplate(w, templateTwoFactor, data)
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:14:47 +00:00
|
|
|
func (s *httpdServer) renderClientTwoFactorRecoveryPage(w http.ResponseWriter, _ *http.Request, error, ip string) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := twoFactorPage{
|
|
|
|
CurrentURL: webClientTwoFactorRecoveryPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
2022-03-26 07:41:50 +00:00
|
|
|
CSRFToken: createCSRFToken(ip),
|
2021-09-04 10:11:04 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateTwoFactorRecovery, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientMFAPage(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
data := clientMFAPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageMFATitle, webClientMFAPath, r),
|
2021-09-04 10:11:04 +00:00
|
|
|
TOTPConfigs: mfa.GetAvailableTOTPConfigNames(),
|
|
|
|
GenerateTOTPURL: webClientTOTPGeneratePath,
|
|
|
|
ValidateTOTPURL: webClientTOTPValidatePath,
|
|
|
|
SaveTOTPURL: webClientTOTPSavePath,
|
|
|
|
RecCodesURL: webClientRecoveryCodesPath,
|
|
|
|
Protocols: dataprovider.MFAProtocols,
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(data.LoggedUser.Username, "")
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-09-04 10:11:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.TOTPConfig = user.Filters.TOTPConfig
|
|
|
|
renderClientTemplate(w, templateClientMFA, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderEditFilePage(w http.ResponseWriter, r *http.Request, fileName, fileData string, readOnly bool) {
|
2021-10-09 12:17:28 +00:00
|
|
|
data := editFilePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientEditFileTitle, webClientEditFilePath, r),
|
2021-10-09 12:17:28 +00:00
|
|
|
Path: fileName,
|
|
|
|
Name: path.Base(fileName),
|
|
|
|
CurrentDir: path.Dir(fileName),
|
2021-12-08 18:25:22 +00:00
|
|
|
FileURL: webClientFilePath,
|
2021-11-25 18:24:32 +00:00
|
|
|
ReadOnly: readOnly,
|
2021-10-09 12:17:28 +00:00
|
|
|
Data: fileData,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientEditFile, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderAddUpdateSharePage(w http.ResponseWriter, r *http.Request, share *dataprovider.Share,
|
2021-11-06 13:13:20 +00:00
|
|
|
error string, isAdd bool) {
|
|
|
|
currentURL := webClientSharePath
|
|
|
|
title := "Add a new share"
|
|
|
|
if !isAdd {
|
|
|
|
currentURL = fmt.Sprintf("%v/%v", webClientSharePath, url.PathEscape(share.ShareID))
|
|
|
|
title = "Update share"
|
|
|
|
}
|
|
|
|
data := clientSharePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(title, currentURL, r),
|
2021-11-06 13:13:20 +00:00
|
|
|
Share: share,
|
|
|
|
Error: error,
|
|
|
|
IsAdd: isAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientShare, data)
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
func getDirMapping(dirName, baseWebPath string) []dirMapping {
|
|
|
|
paths := []dirMapping{}
|
|
|
|
if dirName != "/" {
|
|
|
|
paths = append(paths, dirMapping{
|
|
|
|
DirName: path.Base(dirName),
|
2023-11-04 09:54:52 +00:00
|
|
|
Href: getFileObjectURL("/", dirName, baseWebPath),
|
2022-02-06 15:46:43 +00:00
|
|
|
})
|
|
|
|
for {
|
|
|
|
dirName = path.Dir(dirName)
|
|
|
|
if dirName == "/" || dirName == "." {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
paths = append([]dirMapping{{
|
|
|
|
DirName: path.Base(dirName),
|
|
|
|
Href: getFileObjectURL("/", dirName, baseWebPath)},
|
|
|
|
}, paths...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderSharedFilesPage(w http.ResponseWriter, r *http.Request, dirName, error string,
|
|
|
|
share dataprovider.Share,
|
|
|
|
) {
|
2022-02-06 15:46:43 +00:00
|
|
|
currentURL := path.Join(webClientPubSharesPath, share.ShareID, "browse")
|
2023-11-04 09:54:52 +00:00
|
|
|
baseData := s.getBaseClientPageData(pageExtShareTitle, currentURL, r)
|
|
|
|
baseData.FilesURL = currentURL
|
|
|
|
|
|
|
|
data := filesPage{
|
|
|
|
baseClientPage: baseData,
|
|
|
|
Error: error,
|
|
|
|
CurrentDir: url.QueryEscape(dirName),
|
|
|
|
DownloadURL: path.Join(webClientPubSharesPath, share.ShareID, "partial"),
|
|
|
|
ShareUploadBaseURL: path.Join(webClientPubSharesPath, share.ShareID, url.PathEscape(dirName)),
|
|
|
|
ViewPDFURL: path.Join(webClientPubSharesPath, share.ShareID, "viewpdf"),
|
|
|
|
DirsURL: path.Join(webClientPubSharesPath, share.ShareID, "dirs"),
|
|
|
|
FileURL: "",
|
|
|
|
FileActionsURL: "",
|
|
|
|
CanAddFiles: share.Scope == dataprovider.ShareScopeReadWrite,
|
|
|
|
CanCreateDirs: false,
|
|
|
|
CanRename: false,
|
|
|
|
CanDelete: false,
|
|
|
|
CanDownload: share.Scope != dataprovider.ShareScopeWrite,
|
|
|
|
CanShare: false,
|
|
|
|
HasIntegrations: false,
|
|
|
|
Paths: getDirMapping(dirName, currentURL),
|
|
|
|
QuotaUsage: newUserQuotaUsage(&dataprovider.User{}),
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
renderClientTemplate(w, templateClientFiles, data)
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderUploadToSharePage(w http.ResponseWriter, r *http.Request, share dataprovider.Share) {
|
2022-02-15 18:19:25 +00:00
|
|
|
currentURL := path.Join(webClientPubSharesPath, share.ShareID, "upload")
|
|
|
|
data := shareUploadPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageUploadToShareTitle, currentURL, r),
|
2022-02-15 18:19:25 +00:00
|
|
|
Share: &share,
|
|
|
|
UploadBasePath: path.Join(webClientPubSharesPath, share.ShareID),
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateUploadToShare, data)
|
|
|
|
}
|
|
|
|
|
2023-06-16 19:06:21 +00:00
|
|
|
func (s *httpdServer) renderFilesPage(w http.ResponseWriter, r *http.Request, dirName, error string, user *dataprovider.User,
|
2021-12-03 17:33:08 +00:00
|
|
|
hasIntegrations bool,
|
|
|
|
) {
|
2021-05-06 19:35:43 +00:00
|
|
|
data := filesPage{
|
2023-11-04 09:54:52 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientFilesTitle, webClientFilesPath, r),
|
|
|
|
Error: error,
|
|
|
|
CurrentDir: url.QueryEscape(dirName),
|
|
|
|
DownloadURL: webClientDownloadZipPath,
|
|
|
|
ViewPDFURL: webClientViewPDFPath,
|
|
|
|
DirsURL: webClientDirsPath,
|
|
|
|
FileURL: webClientFilePath,
|
|
|
|
FileActionsURL: webClientFileActionsPath,
|
|
|
|
CanAddFiles: user.CanAddFilesFromWeb(dirName),
|
|
|
|
CanCreateDirs: user.CanAddDirsFromWeb(dirName),
|
|
|
|
CanRename: user.CanRenameFromWeb(dirName, dirName),
|
|
|
|
CanDelete: user.CanDeleteFromWeb(dirName),
|
|
|
|
CanDownload: user.HasPerm(dataprovider.PermDownload, dirName),
|
|
|
|
CanShare: user.CanManageShares(),
|
|
|
|
ShareUploadBaseURL: "",
|
|
|
|
HasIntegrations: hasIntegrations,
|
|
|
|
Paths: getDirMapping(dirName, webClientFilesPath),
|
|
|
|
QuotaUsage: newUserQuotaUsage(user),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientFiles, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientProfilePage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := clientProfilePage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientProfileTitle, webClientProfilePath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, userMerged, err := dataprovider.GetUserVariants(data.LoggedUser.Username, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
data.PublicKeys = user.PublicKeys
|
2021-08-17 16:08:32 +00:00
|
|
|
data.AllowAPIKeyAuth = user.Filters.AllowAPIKeyAuth
|
2021-09-29 16:46:15 +00:00
|
|
|
data.Email = user.Email
|
|
|
|
data.Description = user.Description
|
2022-04-25 13:49:11 +00:00
|
|
|
data.CanSubmit = userMerged.CanChangeAPIKeyAuth() || userMerged.CanManagePublicKeys() || userMerged.CanChangeInfo()
|
2021-09-29 16:46:15 +00:00
|
|
|
renderClientTemplate(w, templateClientProfile, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) renderClientChangePasswordPage(w http.ResponseWriter, r *http.Request, error string) {
|
2021-09-29 16:46:15 +00:00
|
|
|
data := changeClientPasswordPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientChangePwdTitle, webChangeClientPwdPath, r),
|
2021-09-29 16:46:15 +00:00
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClientTemplate(w, templateClientChangePwd, data)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientDownloadZip(w http.ResponseWriter, r *http.Request) {
|
2023-11-04 09:54:52 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxMultipartMem)
|
2021-05-30 21:07:46 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid token claims", "", http.StatusForbidden, nil, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid request", err.Error(), getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-06-05 14:07:09 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2023-11-04 09:54:52 +00:00
|
|
|
files := r.Form.Get("files")
|
2021-05-30 21:07:46 +00:00
|
|
|
var filesList []string
|
|
|
|
err = json.Unmarshal([]byte(files), &filesList)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to get files list", "", http.StatusInternalServerError, err, "")
|
2021-05-30 21:07:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:24:17 +00:00
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"",
|
|
|
|
getCompressedFileName(connection.GetUsername(), filesList)))
|
2021-11-06 13:13:20 +00:00
|
|
|
renderCompressedFiles(w, connection, name, filesList, nil)
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 17:58:35 +00:00
|
|
|
func (s *httpdServer) handleClientSharePartialDownload(w http.ResponseWriter, r *http.Request) {
|
2023-11-04 09:54:52 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxMultipartMem)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid request", err.Error(), getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
2022-09-19 17:58:35 +00:00
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeRead, dataprovider.ShareScopeReadWrite}
|
2023-02-12 07:29:53 +00:00
|
|
|
share, connection, err := s.checkPublicShare(w, r, validScopes)
|
2022-09-19 17:58:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
transferQuota := connection.GetTransferQuota()
|
|
|
|
if !transferQuota.HasDownloadSpace() {
|
|
|
|
err = connection.GetReadQuotaExceededError()
|
|
|
|
connection.Log(logger.LevelInfo, "denying share read due to quota limits")
|
|
|
|
s.renderClientMessagePage(w, r, "Denying share read due to quota limits", "", getMappedStatusCode(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
files := r.Form.Get("files")
|
2022-09-19 17:58:35 +00:00
|
|
|
var filesList []string
|
|
|
|
err = json.Unmarshal([]byte(files), &filesList)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to get files list", "", http.StatusInternalServerError, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dataprovider.UpdateShareLastUse(&share, 1) //nolint:errcheck
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"",
|
|
|
|
getCompressedFileName(fmt.Sprintf("share-%s", share.Name), filesList)))
|
|
|
|
renderCompressedFiles(w, connection, name, filesList, &share)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleShareGetDirContents(w http.ResponseWriter, r *http.Request) {
|
2022-02-06 15:46:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-05-09 17:09:43 +00:00
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeRead, dataprovider.ShareScopeReadWrite}
|
2023-02-12 07:29:53 +00:00
|
|
|
share, connection, err := s.checkPublicShare(w, r, validScopes)
|
2022-02-06 15:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 15:46:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
contents, err := connection.ReadDir(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "Unable to get directory contents", getMappedStatusCode(err))
|
|
|
|
return
|
|
|
|
}
|
2023-06-04 19:45:31 +00:00
|
|
|
results := make([]map[string]any, 0, len(contents))
|
2022-02-06 15:46:43 +00:00
|
|
|
for _, info := range contents {
|
|
|
|
if !info.Mode().IsDir() && !info.Mode().IsRegular() {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-04 19:45:31 +00:00
|
|
|
res := make(map[string]any)
|
2022-02-06 15:46:43 +00:00
|
|
|
if info.IsDir() {
|
|
|
|
res["type"] = "1"
|
|
|
|
res["size"] = ""
|
|
|
|
} else {
|
|
|
|
res["type"] = "2"
|
2023-06-04 19:45:31 +00:00
|
|
|
res["size"] = info.Size()
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
2022-09-19 17:58:35 +00:00
|
|
|
res["meta"] = fmt.Sprintf("%v_%v", res["type"], info.Name())
|
2022-02-06 15:46:43 +00:00
|
|
|
res["name"] = info.Name()
|
|
|
|
res["url"] = getFileObjectURL(share.GetRelativePath(name), info.Name(),
|
|
|
|
path.Join(webClientPubSharesPath, share.ShareID, "browse"))
|
|
|
|
res["last_modified"] = getFileObjectModTime(info.ModTime())
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, results)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUploadToShare(w http.ResponseWriter, r *http.Request) {
|
2022-02-15 18:19:25 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-05-09 17:09:43 +00:00
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeWrite, dataprovider.ShareScopeReadWrite}
|
2023-02-12 07:29:53 +00:00
|
|
|
share, _, err := s.checkPublicShare(w, r, validScopes)
|
2022-02-15 18:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-05-09 17:09:43 +00:00
|
|
|
if share.Scope == dataprovider.ShareScopeReadWrite {
|
|
|
|
http.Redirect(w, r, path.Join(webClientPubSharesPath, share.ShareID, "browse"), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderUploadToSharePage(w, r, share)
|
2022-02-15 18:19:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleShareGetFiles(w http.ResponseWriter, r *http.Request) {
|
2022-02-06 15:46:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-05-09 17:09:43 +00:00
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeRead, dataprovider.ShareScopeReadWrite}
|
2023-02-12 07:29:53 +00:00
|
|
|
share, connection, err := s.checkPublicShare(w, r, validScopes)
|
2022-02-06 15:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 15:46:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
var info os.FileInfo
|
|
|
|
if name == "/" {
|
2022-08-16 15:59:13 +00:00
|
|
|
info = vfs.NewFileInfo(name, true, 0, time.Unix(0, 0), false)
|
2022-02-06 15:46:43 +00:00
|
|
|
} else {
|
|
|
|
info, err = connection.Stat(name, 1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, path.Dir(share.GetRelativePath(name)), err.Error(), share)
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, share.GetRelativePath(name), "", share)
|
2022-02-06 15:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dataprovider.UpdateShareLastUse(&share, 1) //nolint:errcheck
|
2022-09-22 18:41:28 +00:00
|
|
|
if status, err := downloadFile(w, r, connection, name, info, false, &share); err != nil {
|
2022-02-06 15:46:43 +00:00
|
|
|
dataprovider.UpdateShareLastUse(&share, -1) //nolint:errcheck
|
|
|
|
if status > 0 {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderSharedFilesPage(w, r, path.Dir(share.GetRelativePath(name)), err.Error(), share)
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-04 09:54:52 +00:00
|
|
|
func (s *httpdServer) handleShareViewPDF(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeRead, dataprovider.ShareScopeReadWrite}
|
|
|
|
share, _, err := s.checkPublicShare(w, r, validScopes)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := viewPDFPage{
|
|
|
|
Title: path.Base(name),
|
|
|
|
URL: fmt.Sprintf("%s?path=%s&_=%d", path.Join(webClientPubSharesPath, share.ShareID, "getpdf"),
|
|
|
|
url.QueryEscape(name), time.Now().UTC().Unix()),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
Branding: s.binding.Branding.WebClient,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientViewPDF, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleShareGetPDF(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
validScopes := []dataprovider.ShareScope{dataprovider.ShareScopeRead, dataprovider.ShareScopeReadWrite}
|
|
|
|
share, connection, err := s.checkPublicShare(w, r, validScopes)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := validateBrowsableShare(share, connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to validate share", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err := getBrowsableSharedPath(share, r)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid share path", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
info, err := connection.Stat(name, 0)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to get file", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid file", fmt.Sprintf("%q is not a file", name),
|
|
|
|
http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connection.User.CheckFsRoot(connection.ID) //nolint:errcheck
|
|
|
|
if err := s.ensurePDF(w, r, name, connection); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataprovider.UpdateShareLastUse(&share, 1) //nolint:errcheck
|
|
|
|
if _, err := downloadFile(w, r, connection, name, info, true, &share); err != nil {
|
|
|
|
dataprovider.UpdateShareLastUse(&share, -1) //nolint:errcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
func (s *httpdServer) handleClientGetDirContents(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-27 07:40:46 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, nil, "invalid token claims", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2021-05-27 07:40:46 +00:00
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Unable to retrieve your user", getRespStatus(err))
|
2021-05-27 07:40:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-27 07:40:46 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-05-27 07:40:46 +00:00
|
|
|
contents, err := connection.ReadDir(name)
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
sendAPIResponse(w, r, err, "Unable to get directory contents", getMappedStatusCode(err))
|
2021-05-27 07:40:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-04 09:54:52 +00:00
|
|
|
dirTree := r.URL.Query().Get("dirtree") == "1"
|
2023-06-04 19:45:31 +00:00
|
|
|
results := make([]map[string]any, 0, len(contents))
|
2021-05-27 07:40:46 +00:00
|
|
|
for _, info := range contents {
|
2023-06-04 19:45:31 +00:00
|
|
|
res := make(map[string]any)
|
2022-02-06 15:46:43 +00:00
|
|
|
res["url"] = getFileObjectURL(name, info.Name(), webClientFilesPath)
|
2021-05-27 07:40:46 +00:00
|
|
|
if info.IsDir() {
|
|
|
|
res["type"] = "1"
|
|
|
|
res["size"] = ""
|
2023-11-04 09:54:52 +00:00
|
|
|
res["dir_path"] = url.QueryEscape(path.Join(name, info.Name()))
|
2021-05-27 07:40:46 +00:00
|
|
|
} else {
|
2023-11-04 09:54:52 +00:00
|
|
|
if dirTree {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
res["type"] = "2"
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
res["size"] = ""
|
|
|
|
} else {
|
2023-06-04 19:45:31 +00:00
|
|
|
res["size"] = info.Size()
|
2021-10-09 12:17:28 +00:00
|
|
|
if info.Size() < httpdMaxEditFileSize {
|
2023-06-04 19:45:31 +00:00
|
|
|
res["edit_url"] = strings.Replace(res["url"].(string), webClientFilesPath, webClientEditFilePath, 1)
|
2021-12-03 17:33:08 +00:00
|
|
|
}
|
|
|
|
if len(s.binding.WebClientIntegrations) > 0 {
|
|
|
|
extension := path.Ext(info.Name())
|
|
|
|
for idx := range s.binding.WebClientIntegrations {
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(s.binding.WebClientIntegrations[idx].FileExtensions, extension) {
|
2021-12-03 17:33:08 +00:00
|
|
|
res["ext_url"] = s.binding.WebClientIntegrations[idx].URL
|
2021-12-08 18:25:22 +00:00
|
|
|
res["ext_link"] = fmt.Sprintf("%v?path=%v&_=%v", webClientFilePath,
|
|
|
|
url.QueryEscape(path.Join(name, info.Name())), time.Now().UTC().Unix())
|
2021-12-03 17:33:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
}
|
2021-05-27 07:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
res["meta"] = fmt.Sprintf("%v_%v", res["type"], info.Name())
|
2021-05-27 07:40:46 +00:00
|
|
|
res["name"] = info.Name()
|
|
|
|
res["last_modified"] = getFileObjectModTime(info.ModTime())
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, results)
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
func (s *httpdServer) handleClientGetFiles(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-06 19:35:43 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-07-24 18:11:17 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-05-06 19:35:43 +00:00
|
|
|
var info os.FileInfo
|
|
|
|
if name == "/" {
|
2022-08-16 15:59:13 +00:00
|
|
|
info = vfs.NewFileInfo(name, true, 0, time.Unix(0, 0), false)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
|
|
|
info, err = connection.Stat(name, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderFilesPage(w, r, path.Dir(name), fmt.Sprintf("unable to stat file %q: %v", name, err),
|
2023-06-16 19:06:21 +00:00
|
|
|
&user, len(s.binding.WebClientIntegrations) > 0)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2023-06-16 19:06:21 +00:00
|
|
|
s.renderFilesPage(w, r, name, "", &user, len(s.binding.WebClientIntegrations) > 0)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-22 18:41:28 +00:00
|
|
|
if status, err := downloadFile(w, r, connection, name, info, false, nil); err != nil && status != 0 {
|
2021-06-05 14:07:09 +00:00
|
|
|
if status > 0 {
|
|
|
|
if status == http.StatusRequestedRangeNotSatisfiable {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, http.StatusText(status), "", status, err, "")
|
2021-06-05 14:07:09 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-16 19:06:21 +00:00
|
|
|
s.renderFilesPage(w, r, path.Dir(name), err.Error(), &user, len(s.binding.WebClientIntegrations) > 0)
|
2021-06-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientEditFile(w http.ResponseWriter, r *http.Request) {
|
2021-10-09 12:17:28 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2021-10-09 12:17:28 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
2022-02-19 09:53:35 +00:00
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
2022-04-14 17:07:41 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-10-09 12:17:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2022-02-19 09:53:35 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
2021-10-09 12:17:28 +00:00
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
2021-10-09 12:17:28 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
name := connection.User.GetCleanedPath(r.URL.Query().Get("path"))
|
2021-10-09 12:17:28 +00:00
|
|
|
info, err := connection.Stat(name, 0)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to stat file %q", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("The path %q does not point to a file", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.Size() > httpdMaxEditFileSize {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("The file size %v for %q exceeds the maximum allowed size",
|
2021-10-09 12:17:28 +00:00
|
|
|
util.ByteCountIEC(info.Size()), name), "", http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-07 14:23:56 +00:00
|
|
|
connection.User.CheckFsRoot(connection.ID) //nolint:errcheck
|
2021-10-09 12:17:28 +00:00
|
|
|
reader, err := connection.getFileReader(name, 0, r.Method)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to get a reader for the file %q", name), "",
|
2021-10-09 12:17:28 +00:00
|
|
|
getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = io.Copy(&b, reader)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to read the file %q", name), "", http.StatusInternalServerError,
|
2021-10-09 12:17:28 +00:00
|
|
|
nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-29 07:17:24 +00:00
|
|
|
s.renderEditFilePage(w, r, name, b.String(), !user.CanAddFilesFromWeb(path.Dir(name)))
|
2021-10-09 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientAddShareGet(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-08-28 12:41:42 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
|
|
|
return
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2022-08-28 12:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
share := &dataprovider.Share{Scope: dataprovider.ShareScopeRead}
|
2022-08-28 12:41:42 +00:00
|
|
|
if user.Filters.DefaultSharesExpiration > 0 {
|
|
|
|
share.ExpiresAt = util.GetTimeAsMsSinceEpoch(time.Now().Add(24 * time.Hour * time.Duration(user.Filters.DefaultSharesExpiration)))
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
dirName := "/"
|
|
|
|
if _, ok := r.URL.Query()["path"]; ok {
|
|
|
|
dirName = util.CleanPath(r.URL.Query().Get("path"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := r.URL.Query()["files"]; ok {
|
|
|
|
files := r.URL.Query().Get("files")
|
|
|
|
var filesList []string
|
|
|
|
err := json.Unmarshal([]byte(files), &filesList)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Invalid share list", "", http.StatusBadRequest, err, "")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range filesList {
|
|
|
|
if f != "" {
|
|
|
|
share.Paths = append(share.Paths, path.Join(dirName, f))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, "", true)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUpdateShareGet(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
shareID := getURLParam(r, "id")
|
|
|
|
share, err := dataprovider.ShareExists(shareID, claims.Username)
|
|
|
|
if err == nil {
|
|
|
|
share.HideConfidentialData()
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, &share, "", false)
|
2023-01-25 17:49:03 +00:00
|
|
|
} else if errors.Is(err, util.ErrNotFound) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientAddSharePost(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
share, err := getShareFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, err.Error(), true)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
share.ID = 0
|
|
|
|
share.ShareID = util.GenerateUniqueID()
|
|
|
|
share.LastUseAt = 0
|
|
|
|
share.Username = claims.Username
|
2022-02-19 12:31:58 +00:00
|
|
|
if share.Password == "" {
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(claims.Permissions, sdk.WebClientShareNoPasswordDisabled) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not authorized to share files/folders without a password")
|
2022-02-19 12:31:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 17:07:20 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
|
|
|
if err != nil {
|
|
|
|
s.renderAddUpdateSharePage(w, r, share, "Unable to retrieve your user", true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := user.CheckMaxShareExpiration(util.GetTimeFromMsecSinceEpoch(share.ExpiresAt)); err != nil {
|
|
|
|
s.renderAddUpdateSharePage(w, r, share, err.Error(), true)
|
|
|
|
return
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddShare(share, claims.Username, ipAddr, claims.Role)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webClientSharesPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, share, err.Error(), true)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientUpdateSharePost(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
shareID := getURLParam(r, "id")
|
|
|
|
share, err := dataprovider.ShareExists(shareID, claims.Username)
|
2023-01-25 17:49:03 +00:00
|
|
|
if errors.Is(err, util.ErrNotFound) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedShare, err := getShareFromPostFields(r)
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, err.Error(), false)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
updatedShare.ShareID = shareID
|
|
|
|
updatedShare.Username = claims.Username
|
|
|
|
if updatedShare.Password == redactedSecret {
|
|
|
|
updatedShare.Password = share.Password
|
|
|
|
}
|
2022-02-19 12:31:58 +00:00
|
|
|
if updatedShare.Password == "" {
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(claims.Permissions, sdk.WebClientShareNoPasswordDisabled) {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not authorized to share files/folders without a password")
|
2022-02-19 12:31:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 17:07:20 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
|
|
|
if err != nil {
|
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, "Unable to retrieve your user", false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := user.CheckMaxShareExpiration(util.GetTimeFromMsecSinceEpoch(updatedShare.ExpiresAt)); err != nil {
|
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, err.Error(), false)
|
|
|
|
return
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateShare(updatedShare, claims.Username, ipAddr, claims.Role)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, webClientSharesPath, http.StatusSeeOther)
|
|
|
|
} else {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderAddUpdateSharePage(w, r, updatedShare, err.Error(), false)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientGetShares(w http.ResponseWriter, r *http.Request) {
|
2021-11-06 13:13:20 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
limit := defaultQueryLimit
|
|
|
|
if _, ok := r.URL.Query()["qlimit"]; ok {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("qlimit"))
|
|
|
|
if err != nil {
|
|
|
|
limit = defaultQueryLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shares := make([]dataprovider.Share, 0, limit)
|
|
|
|
for {
|
2022-03-19 20:44:27 +00:00
|
|
|
sh, err := dataprovider.GetShares(limit, len(shares), dataprovider.OrderASC, claims.Username)
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderInternalServerErrorPage(w, r, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
shares = append(shares, sh...)
|
|
|
|
if len(sh) < limit {
|
2021-11-06 13:13:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := clientSharesPage{
|
2022-03-19 20:44:27 +00:00
|
|
|
baseClientPage: s.getBaseClientPageData(pageClientSharesTitle, webClientSharesPath, r),
|
2021-11-06 13:13:20 +00:00
|
|
|
Shares: shares,
|
|
|
|
BasePublicSharesURL: webClientPubSharesPath,
|
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientShares, data)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientGetProfile(w http.ResponseWriter, r *http.Request) {
|
2021-09-29 16:46:15 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, "")
|
2021-09-29 16:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientChangePwd(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientChangePasswordPage(w, r, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientProfilePost(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, userMerged, err := dataprovider.GetUserVariants(claims.Username, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, err.Error())
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if !userMerged.CanManagePublicKeys() && !userMerged.CanChangeAPIKeyAuth() && !userMerged.CanChangeInfo() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, "You are not allowed to change anything")
|
2021-08-17 16:08:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if userMerged.CanManagePublicKeys() {
|
2023-11-04 09:54:52 +00:00
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "public_keys[") {
|
|
|
|
r.Form.Add("public_keys", r.Form.Get(k))
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
user.PublicKeys = r.Form["public_keys"]
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if userMerged.CanChangeAPIKeyAuth() {
|
2022-06-11 08:41:34 +00:00
|
|
|
user.Filters.AllowAPIKeyAuth = r.Form.Get("allow_api_key_auth") != ""
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
if userMerged.CanChangeInfo() {
|
2023-06-04 06:45:17 +00:00
|
|
|
user.Email = strings.TrimSpace(r.Form.Get("email"))
|
2021-09-29 16:46:15 +00:00
|
|
|
user.Description = r.Form.Get("description")
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSelf, ipAddr, user.Role)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientProfilePage(w, r, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMessagePage(w, r, "Profile updated", "", http.StatusOK, nil,
|
2021-09-29 16:46:15 +00:00
|
|
|
"Your profile has been successfully updated")
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientMFA(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientMFAPage(w, r)
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactor(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2023-06-08 16:14:47 +00:00
|
|
|
s.renderClientTwoFactorPage(w, r, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactorRecovery(w http.ResponseWriter, r *http.Request) {
|
2021-09-04 10:11:04 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2023-06-08 16:14:47 +00:00
|
|
|
s.renderClientTwoFactorRecoveryPage(w, r, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
func getShareFromPostFields(r *http.Request) (*dataprovider.Share, error) {
|
|
|
|
share := &dataprovider.Share{}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
for k := range r.Form {
|
|
|
|
if strings.HasPrefix(k, "paths[") {
|
|
|
|
r.Form.Add("paths", r.Form.Get(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 06:45:17 +00:00
|
|
|
share.Name = strings.TrimSpace(r.Form.Get("name"))
|
2021-11-06 13:13:20 +00:00
|
|
|
share.Description = r.Form.Get("description")
|
2023-06-04 06:45:17 +00:00
|
|
|
for _, p := range r.Form["paths"] {
|
2023-10-29 06:33:56 +00:00
|
|
|
if strings.TrimSpace(p) != "" {
|
2023-06-04 06:45:17 +00:00
|
|
|
share.Paths = append(share.Paths, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
share.Password = strings.TrimSpace(r.Form.Get("password"))
|
2021-11-06 13:13:20 +00:00
|
|
|
share.AllowFrom = getSliceFromDelimitedValues(r.Form.Get("allowed_ip"), ",")
|
|
|
|
scope, err := strconv.Atoi(r.Form.Get("scope"))
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
share.Scope = dataprovider.ShareScope(scope)
|
|
|
|
maxTokens, err := strconv.Atoi(r.Form.Get("max_tokens"))
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
share.MaxTokens = maxTokens
|
|
|
|
expirationDateMillis := int64(0)
|
2023-06-04 06:45:17 +00:00
|
|
|
expirationDateString := strings.TrimSpace(r.Form.Get("expiration_date"))
|
|
|
|
if expirationDateString != "" {
|
2021-11-06 13:13:20 +00:00
|
|
|
expirationDate, err := time.Parse(webDateTimeFormat, expirationDateString)
|
|
|
|
if err != nil {
|
|
|
|
return share, err
|
|
|
|
}
|
|
|
|
expirationDateMillis = util.GetTimeAsMsSinceEpoch(expirationDate)
|
|
|
|
}
|
|
|
|
share.ExpiresAt = expirationDateMillis
|
|
|
|
return share, nil
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientForgotPwd(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
if !smtp.IsEnabled() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, errors.New("this page does not exist"))
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientForgotPwdPost(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-03-26 07:41:50 +00:00
|
|
|
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, err.Error(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-04 06:45:17 +00:00
|
|
|
username := strings.TrimSpace(r.Form.Get("username"))
|
2021-11-13 12:25:43 +00:00
|
|
|
err = handleForgotPassword(r, username, false)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*util.ValidationError); ok {
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, e.GetErrorString(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-26 07:41:50 +00:00
|
|
|
s.renderClientForgotPwdPage(w, err.Error(), ipAddr)
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, webClientResetPwdPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleWebClientPasswordReset(w http.ResponseWriter, r *http.Request) {
|
2021-11-13 12:25:43 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
if !smtp.IsEnabled() {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientNotFoundPage(w, r, errors.New("this page does not exist"))
|
2021-11-13 12:25:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-08 16:14:47 +00:00
|
|
|
s.renderClientResetPwdPage(w, r, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
2021-11-25 18:24:32 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
func (s *httpdServer) handleClientViewPDF(w http.ResponseWriter, r *http.Request) {
|
2021-11-25 18:24:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
name := r.URL.Query().Get("path")
|
|
|
|
if name == "" {
|
2022-03-19 20:44:27 +00:00
|
|
|
s.renderClientBadRequestPage(w, r, errors.New("no file specified"))
|
2021-11-25 18:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
name = util.CleanPath(name)
|
|
|
|
data := viewPDFPage{
|
|
|
|
Title: path.Base(name),
|
2022-09-22 18:41:28 +00:00
|
|
|
URL: fmt.Sprintf("%s?path=%s&_=%d", webClientGetPDFPath, url.QueryEscape(name), time.Now().UTC().Unix()),
|
2021-11-25 18:24:32 +00:00
|
|
|
StaticURL: webStaticFilesPath,
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: s.binding.Branding.WebClient,
|
2021-11-25 18:24:32 +00:00
|
|
|
}
|
|
|
|
renderClientTemplate(w, templateClientViewPDF, data)
|
|
|
|
}
|
2022-09-22 18:41:28 +00:00
|
|
|
|
|
|
|
func (s *httpdServer) handleClientGetPDF(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
s.renderClientForbiddenPage(w, r, "Invalid token claims")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := r.URL.Query().Get("path")
|
|
|
|
if name == "" {
|
|
|
|
s.renderClientBadRequestPage(w, r, errors.New("no file specified"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = util.CleanPath(name)
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.GetUserWithGroupSettings(claims.Username, "")
|
2022-09-22 18:41:28 +00:00
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to retrieve your user", "", getRespStatus(err), nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connID := xid.New().String()
|
|
|
|
protocol := getProtocolFromRequest(r)
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", protocol, connID)
|
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID, false); err != nil {
|
|
|
|
s.renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
|
|
|
BaseConnection: common.NewBaseConnection(connID, protocol, util.GetHTTPLocalAddress(r),
|
|
|
|
r.RemoteAddr, user),
|
|
|
|
request: r,
|
|
|
|
}
|
|
|
|
if err = common.Connections.Add(connection); err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to add connection", "", http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
|
|
|
info, err := connection.Stat(name, 0)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Unable to get file", "", getRespStatus(err), err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid file", fmt.Sprintf("%q is not a file", name),
|
|
|
|
http.StatusBadRequest, nil, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connection.User.CheckFsRoot(connection.ID) //nolint:errcheck
|
2023-11-04 09:54:52 +00:00
|
|
|
if err := s.ensurePDF(w, r, name, connection); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
downloadFile(w, r, connection, name, info, true, nil) //nolint:errcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) ensurePDF(w http.ResponseWriter, r *http.Request, name string, connection *Connection) error {
|
2022-09-22 18:41:28 +00:00
|
|
|
reader, err := connection.getFileReader(name, 0, r.Method)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, fmt.Sprintf("Unable to get a reader for the file %q", name), "",
|
|
|
|
getRespStatus(err), err, "")
|
2023-11-04 09:54:52 +00:00
|
|
|
return err
|
2022-09-22 18:41:28 +00:00
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = io.CopyN(&b, reader, 128)
|
|
|
|
if err != nil {
|
|
|
|
s.renderClientMessagePage(w, r, "Invalid PDF file", fmt.Sprintf("Unable to validate the file %q as PDF", name),
|
|
|
|
http.StatusBadRequest, nil, "")
|
2023-11-04 09:54:52 +00:00
|
|
|
return err
|
2022-09-22 18:41:28 +00:00
|
|
|
}
|
|
|
|
if ctype := http.DetectContentType(b.Bytes()); ctype != "application/pdf" {
|
|
|
|
connection.Log(logger.LevelDebug, "detected %q content type, expected PDF, file %q", ctype, name)
|
|
|
|
s.renderClientBadRequestPage(w, r, fmt.Errorf("the file %q does not look like a PDF", name))
|
2023-11-04 09:54:52 +00:00
|
|
|
return errors.New("invalid PDF")
|
2022-09-22 18:41:28 +00:00
|
|
|
}
|
2023-11-04 09:54:52 +00:00
|
|
|
return nil
|
2022-09-22 18:41:28 +00:00
|
|
|
}
|
2023-02-12 07:29:53 +00:00
|
|
|
|
|
|
|
func (s *httpdServer) handleClientShareLoginGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, "", util.GetIPFromRemoteAddress(r.RemoteAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleClientShareLoginPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, err.Error(), ipAddr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken), ipAddr); err != nil {
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, err.Error(), ipAddr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shareID := getURLParam(r, "id")
|
|
|
|
share, err := dataprovider.ShareExists(shareID, "")
|
|
|
|
if err != nil {
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, dataprovider.ErrInvalidCredentials.Error(), ipAddr)
|
|
|
|
return
|
|
|
|
}
|
2023-06-04 06:45:17 +00:00
|
|
|
match, err := share.CheckCredentials(strings.TrimSpace(r.Form.Get("share_password")))
|
2023-02-12 07:29:53 +00:00
|
|
|
if !match || err != nil {
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, dataprovider.ErrInvalidCredentials.Error(), ipAddr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: shareID,
|
|
|
|
}
|
|
|
|
err = c.createAndSetCookie(w, r, s.tokenAuth, tokenAudienceWebShare, ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
s.renderShareLoginPage(w, r.RequestURI, common.ErrInternalFailure.Error(), ipAddr)
|
|
|
|
return
|
|
|
|
}
|
2023-03-10 16:30:06 +00:00
|
|
|
next := path.Clean(r.URL.Query().Get("next"))
|
2023-02-12 07:29:53 +00:00
|
|
|
if strings.HasPrefix(next, path.Join(webClientPubSharesPath, share.ShareID)) {
|
|
|
|
http.Redirect(w, r, next, http.StatusFound)
|
2023-06-08 16:14:47 +00:00
|
|
|
return
|
2023-02-12 07:29:53 +00:00
|
|
|
}
|
|
|
|
s.renderClientMessagePage(w, r, "Share Login OK", "Share login successful, you can now use your link",
|
|
|
|
http.StatusOK, nil, "")
|
|
|
|
}
|