2023-03-12 18:00:57 +03:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-05-15 15:52:46 +03:00
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-02 15:10:50 +03:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
|
2022-05-15 15:52:46 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2022-06-08 21:38:03 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2022-05-15 15:52:46 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MediaCleanupPOSTHandler swagger:operation POST /api/v1/admin/media_cleanup mediaCleanup
|
|
|
|
//
|
|
|
|
// Clean up remote media older than the specified number of days.
|
2023-02-11 14:48:38 +03:00
|
|
|
//
|
|
|
|
// Also cleans up unused headers + avatars from the media cache and prunes orphaned items from storage.
|
2022-05-15 15:52:46 +03:00
|
|
|
//
|
2022-09-28 20:30:40 +03:00
|
|
|
// ---
|
|
|
|
// tags:
|
|
|
|
// - admin
|
2022-05-15 15:52:46 +03:00
|
|
|
//
|
2022-09-28 20:30:40 +03:00
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// - application/xml
|
|
|
|
// - application/x-www-form-urlencoded
|
2022-05-15 15:52:46 +03:00
|
|
|
//
|
2022-09-28 20:30:40 +03:00
|
|
|
// produces:
|
|
|
|
// - application/json
|
2022-05-15 15:52:46 +03:00
|
|
|
//
|
2022-09-28 20:30:40 +03:00
|
|
|
// security:
|
|
|
|
// - OAuth2 Bearer:
|
|
|
|
// - admin
|
2022-05-15 15:52:46 +03:00
|
|
|
//
|
2022-09-28 20:30:40 +03:00
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: >-
|
|
|
|
// Echos the number of days requested.
|
|
|
|
// The cleanup is performed asynchronously after the request completes.
|
|
|
|
// '400':
|
|
|
|
// description: bad request
|
|
|
|
// '401':
|
|
|
|
// description: unauthorized
|
|
|
|
// '403':
|
|
|
|
// description: forbidden
|
|
|
|
// '404':
|
|
|
|
// description: not found
|
|
|
|
// '406':
|
|
|
|
// description: not acceptable
|
|
|
|
// '500':
|
|
|
|
// description: internal server error
|
2022-05-15 15:52:46 +03:00
|
|
|
func (m *Module) MediaCleanupPOSTHandler(c *gin.Context) {
|
|
|
|
authed, err := oauth.Authed(c, true, true, true, true)
|
|
|
|
if err != nil {
|
2023-02-02 16:08:13 +03:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
|
2022-05-15 15:52:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:35:05 +03:00
|
|
|
if !*authed.User.Admin {
|
2022-06-08 21:38:03 +03:00
|
|
|
err := fmt.Errorf("user %s not an admin", authed.User.ID)
|
2023-02-02 16:08:13 +03:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
|
2022-05-15 15:52:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:53:29 +03:00
|
|
|
if authed.Account.IsMoving() {
|
|
|
|
apiutil.ForbiddenAfterMove(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:10:50 +03:00
|
|
|
form := &apimodel.MediaCleanupRequest{}
|
2022-05-15 15:52:46 +03:00
|
|
|
if err := c.ShouldBind(form); err != nil {
|
2023-02-02 16:08:13 +03:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
2022-05-15 15:52:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var remoteCacheDays int
|
|
|
|
if form.RemoteCacheDays == nil {
|
2022-05-30 15:41:24 +03:00
|
|
|
remoteCacheDays = config.GetMediaRemoteCacheDays()
|
2022-05-15 15:52:46 +03:00
|
|
|
} else {
|
|
|
|
remoteCacheDays = *form.RemoteCacheDays
|
|
|
|
}
|
|
|
|
if remoteCacheDays < 0 {
|
|
|
|
remoteCacheDays = 0
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:05:26 +03:00
|
|
|
if errWithCode := m.processor.Admin().MediaPrune(c.Request.Context(), remoteCacheDays); errWithCode != nil {
|
2023-02-02 16:08:13 +03:00
|
|
|
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
2022-05-15 15:52:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-27 17:00:57 +03:00
|
|
|
apiutil.JSON(c, http.StatusOK, remoteCacheDays)
|
2022-05-15 15:52:46 +03:00
|
|
|
}
|