Aperçu : détection automatique des fichiers texte et réglages
- Hors liste blanche, reniflage du contenu (UTF-8 valide, sans NUL, peu de caractères de contrôle) : .pem, .csr, fichiers sans extension... servis en text/plain avec les mêmes protections - Langage deviné côté client (highlight.js), sauf blocs PEM/PGP - Paramètres > Aperçu : préférences d'affichage par navigateur (coloration, numéros de ligne, retour à la ligne, Markdown rendu/source, CSV) ; réglages administrateur (détection, plafond 1-50 Mo, extensions texte, extensions exclues, aperçu dans les partages) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4da3db132
commit
ca53cedc32
12 files changed
+806
-85
No files matched your search
@@ -73,14 +73,60 @@ type Settings struct {
|
||||
Registration string `json:"registration"`
|
||||
QuotaMB int64 `json:"quotaMB"`
|
||||
CommonReadonly bool `json:"commonReadonly"`
|
||||
|
||||
Preview PreviewSettings `json:"preview"`
|
||||
}
|
||||
|
||||
// PreviewSettings : réglages de l'aperçu des fichiers (/api/preview).
|
||||
type PreviewSettings struct {
|
||||
// Sniff : un fichier hors liste blanche est prévisualisé si son contenu est
|
||||
// du texte lisible (.pem, .csr, fichiers sans extension...).
|
||||
Sniff bool `json:"sniff"`
|
||||
// MaxTextMB : plafond du texte servi (au-delà, seul le début est affiché).
|
||||
MaxTextMB int `json:"maxTextMB"`
|
||||
// TextExts : extensions toujours affichées en texte, sans reniflage.
|
||||
TextExts []string `json:"textExts"`
|
||||
// ExcludedExts : extensions jamais prévisualisées.
|
||||
ExcludedExts []string `json:"excludedExts"`
|
||||
// Shares : aperçu texte et PDF dans les liens de partage.
|
||||
Shares bool `json:"shares"`
|
||||
}
|
||||
|
||||
// Bornes des réglages de l'aperçu.
|
||||
const (
|
||||
MaxPreviewTextMB = 50
|
||||
maxPreviewExts = 100
|
||||
maxPreviewExtLen = 16
|
||||
)
|
||||
|
||||
// DefaultPreview : reniflage actif, 2 Mo de texte, aperçu dans les partages.
|
||||
var DefaultPreview = PreviewSettings{Sniff: true, MaxTextMB: 2, TextExts: []string{}, ExcludedExts: []string{}, Shares: true}
|
||||
|
||||
// previewDisk : champs absents = valeur par défaut.
|
||||
type previewDisk struct {
|
||||
Sniff *bool `json:"sniff,omitempty"`
|
||||
MaxTextMB *int `json:"maxTextMB,omitempty"`
|
||||
TextExts []string `json:"textExts,omitempty"`
|
||||
ExcludedExts []string `json:"excludedExts,omitempty"`
|
||||
Shares *bool `json:"shares,omitempty"`
|
||||
}
|
||||
|
||||
// PreviewPatch : seuls les champs non nil sont modifiés.
|
||||
type PreviewPatch struct {
|
||||
Sniff *bool `json:"sniff"`
|
||||
MaxTextMB *int `json:"maxTextMB"`
|
||||
TextExts *[]string `json:"textExts"`
|
||||
ExcludedExts *[]string `json:"excludedExts"`
|
||||
Shares *bool `json:"shares"`
|
||||
}
|
||||
|
||||
type settingsDisk struct {
|
||||
Title string `json:"title"`
|
||||
TrashDays *int `json:"trashDays,omitempty"`
|
||||
Registration *string `json:"registration,omitempty"`
|
||||
QuotaMB *int64 `json:"quotaMB,omitempty"`
|
||||
CommonReadonly *bool `json:"commonReadonly,omitempty"`
|
||||
Preview *previewDisk `json:"preview,omitempty"`
|
||||
Title string `json:"title"`
|
||||
TrashDays *int `json:"trashDays,omitempty"`
|
||||
Registration *string `json:"registration,omitempty"`
|
||||
QuotaMB *int64 `json:"quotaMB,omitempty"`
|
||||
CommonReadonly *bool `json:"commonReadonly,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Store) readSettingsDisk() (settingsDisk, error) {
|
||||
@@ -148,6 +194,24 @@ func (s *Store) readSettings() (Settings, error) {
|
||||
if d.CommonReadonly != nil {
|
||||
st.CommonReadonly = *d.CommonReadonly
|
||||
}
|
||||
st.Preview = DefaultPreview
|
||||
if p := d.Preview; p != nil {
|
||||
if p.Sniff != nil {
|
||||
st.Preview.Sniff = *p.Sniff
|
||||
}
|
||||
if p.MaxTextMB != nil && *p.MaxTextMB >= 1 && *p.MaxTextMB <= MaxPreviewTextMB {
|
||||
st.Preview.MaxTextMB = *p.MaxTextMB
|
||||
}
|
||||
if p.TextExts != nil {
|
||||
st.Preview.TextExts = p.TextExts
|
||||
}
|
||||
if p.ExcludedExts != nil {
|
||||
st.Preview.ExcludedExts = p.ExcludedExts
|
||||
}
|
||||
if p.Shares != nil {
|
||||
st.Preview.Shares = *p.Shares
|
||||
}
|
||||
}
|
||||
if fi, err := os.Stat(filepath.Join(s.root, logoFile)); err == nil {
|
||||
st.Logo = strconv.FormatInt(fi.ModTime().UnixNano(), 36)
|
||||
}
|
||||
@@ -183,6 +247,70 @@ func (s *Store) WriteCommonReadonly(ro bool) (Settings, error) {
|
||||
return s.updateSettings(func(d *settingsDisk) { d.CommonReadonly = &ro })
|
||||
}
|
||||
|
||||
// WritePreview modifie les réglages de l'aperçu (champs non nil seulement).
|
||||
// Les extensions sont normalisées : minuscules, sans point, [a-z0-9_+-].
|
||||
func (s *Store) WritePreview(p PreviewPatch) (Settings, error) {
|
||||
if p.MaxTextMB != nil && (*p.MaxTextMB < 1 || *p.MaxTextMB > MaxPreviewTextMB) {
|
||||
return Settings{}, fmt.Errorf("%w : plafond de 1 à %d Mo", ErrInvalidPath, MaxPreviewTextMB)
|
||||
}
|
||||
var text, excl []string
|
||||
var err error
|
||||
if p.TextExts != nil {
|
||||
if text, err = NormalizeExts(*p.TextExts); err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
}
|
||||
if p.ExcludedExts != nil {
|
||||
if excl, err = NormalizeExts(*p.ExcludedExts); err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
}
|
||||
return s.updateSettings(func(d *settingsDisk) {
|
||||
if d.Preview == nil {
|
||||
d.Preview = &previewDisk{}
|
||||
}
|
||||
if p.Sniff != nil {
|
||||
d.Preview.Sniff = p.Sniff
|
||||
}
|
||||
if p.MaxTextMB != nil {
|
||||
d.Preview.MaxTextMB = p.MaxTextMB
|
||||
}
|
||||
if p.TextExts != nil {
|
||||
d.Preview.TextExts = text
|
||||
}
|
||||
if p.ExcludedExts != nil {
|
||||
d.Preview.ExcludedExts = excl
|
||||
}
|
||||
if p.Shares != nil {
|
||||
d.Preview.Shares = p.Shares
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// NormalizeExts nettoie une liste d'extensions (« .PEM », « crt »...) : minuscules,
|
||||
// sans point, sans doublon ; refuse les caractères hors [a-z0-9_+-].
|
||||
func NormalizeExts(in []string) ([]string, error) {
|
||||
out := []string{}
|
||||
seen := map[string]bool{}
|
||||
for _, e := range in {
|
||||
e = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(e)), ".")
|
||||
if e == "" || seen[e] {
|
||||
continue
|
||||
}
|
||||
if len(e) > maxPreviewExtLen || strings.IndexFunc(e, func(r rune) bool {
|
||||
return !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '_' || r == '+' || r == '-')
|
||||
}) >= 0 {
|
||||
return nil, fmt.Errorf("%w : extension %q", ErrInvalidPath, e)
|
||||
}
|
||||
seen[e] = true
|
||||
out = append(out, e)
|
||||
}
|
||||
if len(out) > maxPreviewExts {
|
||||
return nil, fmt.Errorf("%w : %d extensions au plus", ErrInvalidPath, maxPreviewExts)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// WriteTitle enregistre le titre affiché ("" = titre par défaut).
|
||||
func (s *Store) WriteTitle(title string) (Settings, error) {
|
||||
title = strings.TrimSpace(title)
|
||||
|
||||
Reference in new issue
Block a user