2021-02-09 05:58:31 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
package export_process
|
|
|
|
|
|
|
|
|
|
import (
|
2022-12-13 14:36:40 -05:00
|
|
|
"context"
|
2021-02-09 05:58:31 -05:00
|
|
|
"io"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
2024-06-05 12:58:04 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/configservice"
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2023-09-05 03:47:30 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/v8/channels/jobs"
|
2021-02-09 05:58:31 -05:00
|
|
|
)
|
|
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
type AppIface interface {
|
|
|
|
|
configservice.ConfigService
|
2023-07-19 16:01:39 -04:00
|
|
|
WriteExportFileContext(ctx context.Context, fr io.Reader, path string) (int64, *model.AppError)
|
2025-09-10 09:11:32 -04:00
|
|
|
BulkExport(rctx request.CTX, writer io.Writer, outPath string, job *model.Job, opts model.BulkExportOpts) *model.AppError
|
2022-08-02 11:18:54 -04:00
|
|
|
Log() *mlog.Logger
|
2021-02-09 05:58:31 -05:00
|
|
|
}
|
|
|
|
|
|
2023-09-07 02:50:22 -04:00
|
|
|
func MakeWorker(jobServer *jobs.JobServer, app AppIface) *jobs.SimpleWorker {
|
|
|
|
|
const workerName = "ExportProcess"
|
|
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
isEnabled := func(cfg *model.Config) bool { return true }
|
2023-10-09 10:04:55 -04:00
|
|
|
execute := func(logger mlog.LoggerIFace, job *model.Job) error {
|
|
|
|
|
defer jobServer.HandleJobPanic(logger, job)
|
2022-10-27 05:08:13 -04:00
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
opts := model.BulkExportOpts{
|
|
|
|
|
CreateArchive: true,
|
2021-02-09 05:58:31 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
includeAttachments, ok := job.Data["include_attachments"]
|
|
|
|
|
if ok && includeAttachments == "true" {
|
|
|
|
|
opts.IncludeAttachments = true
|
|
|
|
|
}
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2023-08-25 20:55:47 -04:00
|
|
|
includeArchivedChannels, ok := job.Data["include_archived_channels"]
|
|
|
|
|
if ok && includeArchivedChannels == "true" {
|
|
|
|
|
opts.IncludeArchivedChannels = true
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 16:29:45 -05:00
|
|
|
includeProfilePictures, ok := job.Data["include_profile_pictures"]
|
|
|
|
|
if ok && includeProfilePictures == "true" {
|
|
|
|
|
opts.IncludeProfilePictures = true
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 10:43:25 -04:00
|
|
|
includeRolesAndSchemes, ok := job.Data["include_roles_and_schemes"]
|
|
|
|
|
if ok && includeRolesAndSchemes == "true" {
|
|
|
|
|
opts.IncludeRolesAndSchemes = true
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
outPath := *app.Config().ExportSettings.Directory
|
2022-07-20 04:58:17 -04:00
|
|
|
exportFilename := job.Id + "_export.zip"
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
rd, wr := io.Pipe()
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
go func() {
|
2023-07-19 16:01:39 -04:00
|
|
|
_, appErr := app.WriteExportFileContext(context.Background(), rd, filepath.Join(outPath, exportFilename))
|
2022-12-16 10:29:26 -05:00
|
|
|
if appErr != nil {
|
|
|
|
|
// we close the reader here to prevent a deadlock when the bulk exporter tries to
|
|
|
|
|
// write into the pipe while app.WriteFile has already returned. The error will be
|
|
|
|
|
// returned by the writer part of the pipe when app.BulkExport tries to call
|
|
|
|
|
// wr.Write() on it.
|
|
|
|
|
rd.CloseWithError(appErr) // CloseWithError never returns an error
|
|
|
|
|
}
|
2022-02-14 12:21:18 -05:00
|
|
|
}()
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2023-10-09 10:04:55 -04:00
|
|
|
appErr := app.BulkExport(request.EmptyContext(logger), wr, outPath, job, opts)
|
2022-12-16 10:29:26 -05:00
|
|
|
wr.Close() // Close never returns an error
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
if appErr != nil {
|
|
|
|
|
return appErr
|
|
|
|
|
}
|
2021-02-09 05:58:31 -05:00
|
|
|
|
2022-02-14 12:21:18 -05:00
|
|
|
return nil
|
2021-02-09 05:58:31 -05:00
|
|
|
}
|
2023-09-07 02:50:22 -04:00
|
|
|
worker := jobs.NewSimpleWorker(workerName, jobServer, execute, isEnabled)
|
2022-02-14 12:21:18 -05:00
|
|
|
return worker
|
2021-02-09 05:58:31 -05:00
|
|
|
}
|