Files
upstream-mattermost/server/channels/app/file_helper.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

211 lines
7.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"net/http"
"github.com/mattermost/mattermost/server/public/model"
)
// removeInaccessibleContentFromFilesSlice removes content from the files beyond the cloud plan's limit
// and also returns the firstInaccessibleFileTime
func (a *App) removeInaccessibleContentFromFilesSlice(files []*model.FileInfo) (int64, *model.AppError) {
if len(files) == 0 {
return 0, nil
}
lastAccessibleFileTime, appErr := a.GetLastAccessibleFileTime()
if appErr != nil {
return 0, model.NewAppError("removeInaccessibleFileListContent", "app.last_accessible_file.app_error", nil, "", http.StatusInternalServerError).Wrap(appErr)
}
if lastAccessibleFileTime == 0 {
// No need to remove content, all files are accessible
return 0, nil
}
var firstInaccessibleFileTime int64
for _, file := range files {
if createAt := file.CreateAt; createAt < lastAccessibleFileTime {
file.MakeContentInaccessible()
if createAt > firstInaccessibleFileTime {
firstInaccessibleFileTime = createAt
}
}
}
return firstInaccessibleFileTime, nil
}
// filterInaccessibleFiles filters out the files, past the cloud limit
func (a *App) filterInaccessibleFiles(fileList *model.FileInfoList, options filterFileOptions) *model.AppError {
if fileList == nil || fileList.FileInfos == nil || len(fileList.FileInfos) == 0 {
return nil
}
lastAccessibleFileTime, appErr := a.GetLastAccessibleFileTime()
if appErr != nil {
return model.NewAppError("filterInaccessibleFiles", "app.last_accessible_file.app_error", nil, "", http.StatusInternalServerError).Wrap(appErr)
}
if lastAccessibleFileTime == 0 {
// No need to filter, all files are accessible
return nil
}
if len(fileList.FileInfos) == len(fileList.Order) && options.assumeSortedCreatedAt {
lenFiles := len(fileList.FileInfos)
getCreateAt := func(i int) int64 { return fileList.FileInfos[fileList.Order[i]].CreateAt }
bounds := getTimeSortedPostAccessibleBounds(lastAccessibleFileTime, lenFiles, getCreateAt)
if bounds.allAccessible(lenFiles) {
return nil
}
if bounds.noAccessible() {
if lenFiles > 0 {
firstFileCreatedAt := fileList.FileInfos[fileList.Order[0]].CreateAt
lastFileCreatedAt := fileList.FileInfos[fileList.Order[lenFiles-1]].CreateAt
fileList.FirstInaccessibleFileTime = max(firstFileCreatedAt, lastFileCreatedAt)
}
fileList.FileInfos = map[string]*model.FileInfo{}
fileList.Order = []string{}
return nil
}
startInaccessibleIndex, endInaccessibleIndex := bounds.getInaccessibleRange(len(fileList.Order))
startInaccessibleCreatedAt := fileList.FileInfos[fileList.Order[startInaccessibleIndex]].CreateAt
endInaccessibleCreatedAt := fileList.FileInfos[fileList.Order[endInaccessibleIndex]].CreateAt
fileList.FirstInaccessibleFileTime = max(startInaccessibleCreatedAt, endInaccessibleCreatedAt)
files := fileList.FileInfos
order := fileList.Order
accessibleCount := bounds.end - bounds.start + 1
inaccessibleCount := lenFiles - accessibleCount
// Linearly cover shorter route to traverse files map
if inaccessibleCount < accessibleCount {
for i := range bounds.start {
delete(files, order[i])
}
for i := bounds.end + 1; i < lenFiles; i++ {
delete(files, order[i])
}
} else {
accessibleFiles := make(map[string]*model.FileInfo, accessibleCount)
for i := bounds.start; i <= bounds.end; i++ {
accessibleFiles[order[i]] = files[order[i]]
}
fileList.FileInfos = accessibleFiles
}
fileList.Order = fileList.Order[bounds.start : bounds.end+1]
} else {
linearFilterFileList(fileList, lastAccessibleFileTime)
}
return nil
}
// isInaccessibleFile indicates if the file is past the cloud plan's limit.
func (a *App) isInaccessibleFile(file *model.FileInfo) (int64, *model.AppError) {
if file == nil {
return 0, nil
}
fl := &model.FileInfoList{
Order: []string{file.Id},
FileInfos: map[string]*model.FileInfo{file.Id: file},
}
appErr := a.filterInaccessibleFiles(fl, filterFileOptions{assumeSortedCreatedAt: true})
return fl.FirstInaccessibleFileTime, appErr
}
// getFilteredAccessibleFiles returns accessible files filtered as per the cloud plan's limit and also indicates if there were any inaccessible files
func (a *App) getFilteredAccessibleFiles(files []*model.FileInfo, options filterFileOptions) ([]*model.FileInfo, int64, *model.AppError) {
if len(files) == 0 {
return files, 0, nil
}
filteredFiles := []*model.FileInfo{}
lastAccessibleFileTime, appErr := a.GetLastAccessibleFileTime()
if appErr != nil {
return filteredFiles, 0, model.NewAppError("getFilteredAccessibleFiles", "app.last_accessible_file.app_error", nil, "", http.StatusInternalServerError).Wrap(appErr)
} else if lastAccessibleFileTime == 0 {
// No need to filter, all files are accessible
return files, 0, nil
}
if options.assumeSortedCreatedAt {
lenFiles := len(files)
getCreateAt := func(i int) int64 { return files[i].CreateAt }
bounds := getTimeSortedPostAccessibleBounds(lastAccessibleFileTime, lenFiles, getCreateAt)
if bounds.allAccessible(lenFiles) {
return files, 0, nil
}
if bounds.noAccessible() {
var firstInaccessibleFileTime int64
if lenFiles > 0 {
firstFileCreatedAt := files[0].CreateAt
lastFileCreatedAt := files[len(files)-1].CreateAt
firstInaccessibleFileTime = max(firstFileCreatedAt, lastFileCreatedAt)
}
return filteredFiles, firstInaccessibleFileTime, nil
}
startInaccessibleIndex, endInaccessibleIndex := bounds.getInaccessibleRange(len(files))
firstFileCreatedAt := files[startInaccessibleIndex].CreateAt
lastFileCreatedAt := files[endInaccessibleIndex].CreateAt
firstInaccessibleFileTime := max(firstFileCreatedAt, lastFileCreatedAt)
filteredFiles = files[bounds.start : bounds.end+1]
return filteredFiles, firstInaccessibleFileTime, nil
}
filteredFiles, firstInaccessibleFileTime := linearFilterFilesSlice(files, lastAccessibleFileTime)
return filteredFiles, firstInaccessibleFileTime, nil
}
type filterFileOptions struct {
assumeSortedCreatedAt bool
}
// linearFilterFileList make no assumptions about ordering, go through files one by one
// this is the slower fallback that is still safe
// if we can not assume files are ordered by CreatedAt
func linearFilterFileList(fileList *model.FileInfoList, earliestAccessibleTime int64) {
files := fileList.FileInfos
order := fileList.Order
n := 0
for i, fileID := range order {
if createAt := files[fileID].CreateAt; createAt >= earliestAccessibleTime {
order[n] = order[i]
n++
} else {
if createAt > fileList.FirstInaccessibleFileTime {
fileList.FirstInaccessibleFileTime = createAt
}
delete(files, fileID)
}
}
fileList.Order = order[:n]
}
// linearFilterFilesSlice make no assumptions about ordering, go through files one by one
// this is the slower fallback that is still safe
// if we can not assume files are ordered by CreatedAt
func linearFilterFilesSlice(files []*model.FileInfo, earliestAccessibleTime int64) ([]*model.FileInfo, int64) {
var firstInaccessibleFileTime int64
n := 0
for i := range files {
if createAt := files[i].CreateAt; createAt >= earliestAccessibleTime {
files[n] = files[i]
n++
} else {
if createAt > firstInaccessibleFileTime {
firstInaccessibleFileTime = createAt
}
}
}
return files[:n], firstInaccessibleFileTime
}