2017-04-12 08:27:57 -04:00
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
2017-02-17 11:57:19 +01:00
// See License.txt for license information.
package api4
import (
2017-06-19 13:46:51 -04:00
"bytes"
2018-12-17 08:51:46 -08:00
"encoding/json"
2018-04-27 12:49:45 -07:00
"fmt"
2017-06-19 13:46:51 -04:00
"io"
2017-02-17 11:57:19 +01:00
"net/http"
2017-05-31 06:47:27 +02:00
"runtime"
2017-02-17 11:57:19 +01:00
2018-04-27 12:49:45 -07:00
"github.com/mattermost/mattermost-server/mlog"
2017-09-06 23:05:10 -07:00
"github.com/mattermost/mattermost-server/model"
2018-09-20 19:07:03 +02:00
"github.com/mattermost/mattermost-server/services/filesstore"
2019-01-27 18:24:46 -08:00
"github.com/mattermost/mattermost-server/utils"
2017-02-17 11:57:19 +01:00
)
2019-01-27 18:24:46 -08:00
const REDIRECT_LOCATION_CACHE_SIZE = 10000
var redirectLocationDataCache = utils . NewLru ( REDIRECT_LOCATION_CACHE_SIZE )
2017-09-22 12:54:27 -05:00
func ( api * API ) InitSystem ( ) {
api . BaseRoutes . System . Handle ( "/ping" , api . ApiHandler ( getSystemPing ) ) . Methods ( "GET" )
2017-05-31 06:47:27 +02:00
2018-03-22 06:53:43 -07:00
api . BaseRoutes . System . Handle ( "/timezones" , api . ApiSessionRequired ( getSupportedTimezones ) ) . Methods ( "GET" )
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/config" , api . ApiSessionRequired ( getConfig ) ) . Methods ( "GET" )
api . BaseRoutes . ApiRoot . Handle ( "/config" , api . ApiSessionRequired ( updateConfig ) ) . Methods ( "PUT" )
api . BaseRoutes . ApiRoot . Handle ( "/config/reload" , api . ApiSessionRequired ( configReload ) ) . Methods ( "POST" )
api . BaseRoutes . ApiRoot . Handle ( "/config/client" , api . ApiHandler ( getClientConfig ) ) . Methods ( "GET" )
2018-04-09 12:16:11 -04:00
api . BaseRoutes . ApiRoot . Handle ( "/config/environment" , api . ApiSessionRequired ( getEnvironmentConfig ) ) . Methods ( "GET" )
2017-03-27 09:19:53 -04:00
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/license" , api . ApiSessionRequired ( addLicense ) ) . Methods ( "POST" )
api . BaseRoutes . ApiRoot . Handle ( "/license" , api . ApiSessionRequired ( removeLicense ) ) . Methods ( "DELETE" )
api . BaseRoutes . ApiRoot . Handle ( "/license/client" , api . ApiHandler ( getClientLicense ) ) . Methods ( "GET" )
2017-03-27 09:19:53 -04:00
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/audits" , api . ApiSessionRequired ( getAudits ) ) . Methods ( "GET" )
api . BaseRoutes . ApiRoot . Handle ( "/email/test" , api . ApiSessionRequired ( testEmail ) ) . Methods ( "POST" )
2018-03-01 00:12:11 +01:00
api . BaseRoutes . ApiRoot . Handle ( "/file/s3_test" , api . ApiSessionRequired ( testS3 ) ) . Methods ( "POST" )
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/database/recycle" , api . ApiSessionRequired ( databaseRecycle ) ) . Methods ( "POST" )
api . BaseRoutes . ApiRoot . Handle ( "/caches/invalidate" , api . ApiSessionRequired ( invalidateCaches ) ) . Methods ( "POST" )
2017-03-16 14:59:44 -04:00
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/logs" , api . ApiSessionRequired ( getLogs ) ) . Methods ( "GET" )
2017-10-20 20:26:12 -04:00
api . BaseRoutes . ApiRoot . Handle ( "/logs" , api . ApiHandler ( postLog ) ) . Methods ( "POST" )
2017-06-19 16:35:53 -04:00
2017-09-22 12:54:27 -05:00
api . BaseRoutes . ApiRoot . Handle ( "/analytics/old" , api . ApiSessionRequired ( getAnalytics ) ) . Methods ( "GET" )
2018-08-24 08:49:31 -04:00
api . BaseRoutes . ApiRoot . Handle ( "/redirect_location" , api . ApiSessionRequiredTrustRequester ( getRedirectLocation ) ) . Methods ( "GET" )
2017-02-17 11:57:19 +01:00
}
func getSystemPing ( c * Context , w http . ResponseWriter , r * http . Request ) {
2017-05-31 06:47:27 +02:00
actualGoroutines := runtime . NumGoroutine ( )
2017-10-18 15:36:43 -07:00
if * c . App . Config ( ) . ServiceSettings . GoroutineHealthThreshold <= 0 || actualGoroutines <= * c . App . Config ( ) . ServiceSettings . GoroutineHealthThreshold {
2017-08-28 09:22:54 -07:00
m := make ( map [ string ] string )
m [ model . STATUS ] = model . STATUS_OK
2017-10-18 15:36:43 -07:00
reqs := c . App . Config ( ) . ClientRequirements
2017-08-28 09:22:54 -07:00
m [ "AndroidLatestVersion" ] = reqs . AndroidLatestVersion
m [ "AndroidMinVersion" ] = reqs . AndroidMinVersion
m [ "DesktopLatestVersion" ] = reqs . DesktopLatestVersion
m [ "DesktopMinVersion" ] = reqs . DesktopMinVersion
m [ "IosLatestVersion" ] = reqs . IosLatestVersion
m [ "IosMinVersion" ] = reqs . IosMinVersion
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
2017-05-31 06:47:27 +02:00
} else {
rdata := map [ string ] string { }
rdata [ "status" ] = "unhealthy"
2018-04-27 12:49:45 -07:00
mlog . Warn ( fmt . Sprintf ( "The number of running goroutines is over the health threshold %v of %v" , actualGoroutines , * c . App . Config ( ) . ServiceSettings . GoroutineHealthThreshold ) )
2017-05-31 06:47:27 +02:00
w . WriteHeader ( http . StatusInternalServerError )
w . Write ( [ ] byte ( model . MapToJson ( rdata ) ) )
}
2017-02-17 11:57:19 +01:00
}
2017-03-13 13:27:27 +01:00
2017-03-13 16:09:00 +01:00
func testEmail ( c * Context , w http . ResponseWriter , r * http . Request ) {
2017-06-14 08:56:56 -04:00
cfg := model . ConfigFromJson ( r . Body )
if cfg == nil {
2017-10-18 15:36:43 -07:00
cfg = c . App . Config ( )
2017-06-14 08:56:56 -04:00
}
2017-03-13 16:09:00 +01:00
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-13 16:09:00 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2018-11-28 10:56:21 -08:00
err := c . App . TestEmail ( c . App . Session . UserId , cfg )
2017-03-13 16:09:00 +01:00
if err != nil {
c . Err = err
return
}
ReturnStatusOK ( w )
}
2017-03-13 13:27:27 +01:00
func getConfig ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-13 13:27:27 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2019-02-12 08:37:54 -05:00
cfg := c . App . GetSanitizedConfig ( )
2017-03-13 13:27:27 +01:00
w . Header ( ) . Set ( "Cache-Control" , "no-cache, no-store, must-revalidate" )
w . Write ( [ ] byte ( cfg . ToJson ( ) ) )
}
2017-03-13 16:47:33 +01:00
2017-03-14 13:52:27 +01:00
func configReload ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-14 13:52:27 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-19 18:31:35 -05:00
c . App . ReloadConfig ( )
2017-03-14 13:52:27 +01:00
w . Header ( ) . Set ( "Cache-Control" , "no-cache, no-store, must-revalidate" )
ReturnStatusOK ( w )
}
2017-03-20 13:37:34 +01:00
func updateConfig ( c * Context , w http . ResponseWriter , r * http . Request ) {
cfg := model . ConfigFromJson ( r . Body )
if cfg == nil {
c . SetInvalidParam ( "config" )
return
}
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-20 13:37:34 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2018-02-13 11:08:49 -05:00
// Do not allow plugin uploads to be toggled through the API
2019-02-12 08:37:54 -05:00
cfg . PluginSettings . EnableUploads = c . App . Config ( ) . PluginSettings . EnableUploads
2018-02-13 11:08:49 -05:00
2018-11-28 14:01:29 +00:00
// If the Message Export feature has been toggled in the System Console, rewrite the ExportFromTimestamp field to an
// appropriate value. The rewriting occurs here to ensure it doesn't affect values written to the config file
// directly and not through the System Console UI.
2019-02-12 08:37:54 -05:00
if * cfg . MessageExportSettings . EnableExport != * c . App . Config ( ) . MessageExportSettings . EnableExport {
2018-11-28 14:01:29 +00:00
if * cfg . MessageExportSettings . EnableExport && * cfg . MessageExportSettings . ExportFromTimestamp == int64 ( 0 ) {
// When the feature is toggled on, use the current timestamp as the start time for future exports.
cfg . MessageExportSettings . ExportFromTimestamp = model . NewInt64 ( model . GetMillis ( ) )
} else if ! * cfg . MessageExportSettings . EnableExport {
// When the feature is disabled, reset the timestamp so that the timestamp will be set if
// the feature is re-enabled from the System Console in future.
cfg . MessageExportSettings . ExportFromTimestamp = model . NewInt64 ( 0 )
}
}
2017-09-19 18:31:35 -05:00
err := c . App . SaveConfig ( cfg , true )
2017-03-20 13:37:34 +01:00
if err != nil {
c . Err = err
return
}
c . LogAudit ( "updateConfig" )
2019-02-12 08:37:54 -05:00
cfg = c . App . GetSanitizedConfig ( )
2017-03-20 13:37:34 +01:00
w . Header ( ) . Set ( "Cache-Control" , "no-cache, no-store, must-revalidate" )
w . Write ( [ ] byte ( cfg . ToJson ( ) ) )
}
2017-03-21 09:06:08 -04:00
func getAudits ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-21 09:06:08 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-06 17:12:54 -05:00
audits , err := c . App . GetAuditsPage ( "" , c . Params . Page , c . Params . PerPage )
2017-03-21 09:06:08 -04:00
if err != nil {
c . Err = err
return
}
w . Write ( [ ] byte ( audits . ToJson ( ) ) )
}
2017-03-13 16:47:33 +01:00
func databaseRecycle ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-13 16:47:33 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-06 17:12:54 -05:00
c . App . RecycleDatabaseConnection ( )
2017-03-13 16:47:33 +01:00
ReturnStatusOK ( w )
}
2017-03-14 17:06:07 +01:00
func invalidateCaches ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-14 17:06:07 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-06 17:12:54 -05:00
err := c . App . InvalidateAllCaches ( )
2017-03-14 17:06:07 +01:00
if err != nil {
c . Err = err
return
}
w . Header ( ) . Set ( "Cache-Control" , "no-cache, no-store, must-revalidate" )
ReturnStatusOK ( w )
}
2017-03-16 14:59:44 -04:00
func getLogs ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-03-16 14:59:44 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-12-14 04:04:55 +09:00
lines , err := c . App . GetLogs ( c . Params . Page , c . Params . LogsPerPage )
2017-03-16 14:59:44 -04:00
if err != nil {
c . Err = err
return
}
w . Write ( [ ] byte ( model . ArrayToJson ( lines ) ) )
}
2017-03-27 09:19:53 -04:00
2017-04-21 11:16:35 +02:00
func postLog ( c * Context , w http . ResponseWriter , r * http . Request ) {
2017-10-20 20:26:12 -04:00
forceToDebug := false
if ! * c . App . Config ( ) . ServiceSettings . EnableDeveloper {
2018-11-28 10:56:21 -08:00
if c . App . Session . UserId == "" {
2017-10-20 20:26:12 -04:00
c . Err = model . NewAppError ( "postLog" , "api.context.permissions.app_error" , nil , "" , http . StatusForbidden )
return
}
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-10-20 20:26:12 -04:00
forceToDebug = true
}
2017-04-21 11:16:35 +02:00
}
m := model . MapFromJson ( r . Body )
lvl := m [ "level" ]
msg := m [ "message" ]
if len ( msg ) > 400 {
msg = msg [ 0 : 399 ]
}
2017-10-20 20:26:12 -04:00
if ! forceToDebug && lvl == "ERROR" {
2017-04-21 11:16:35 +02:00
err := & model . AppError { }
err . Message = msg
err . Id = msg
err . Where = "client"
c . LogError ( err )
} else {
2018-04-27 12:49:45 -07:00
mlog . Debug ( fmt . Sprint ( msg ) )
2017-04-21 11:16:35 +02:00
}
m [ "message" ] = msg
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
}
2017-03-27 09:19:53 -04:00
func getClientConfig ( c * Context , w http . ResponseWriter , r * http . Request ) {
format := r . URL . Query ( ) . Get ( "format" )
if format == "" {
c . Err = model . NewAppError ( "getClientConfig" , "api.config.client.old_format.app_error" , nil , "" , http . StatusNotImplemented )
return
}
if format != "old" {
c . SetInvalidParam ( "format" )
return
}
2018-06-18 12:39:22 -04:00
var config map [ string ] string
2018-11-28 10:56:21 -08:00
if len ( c . App . Session . UserId ) == 0 {
2018-06-18 12:39:22 -04:00
config = c . App . LimitedClientConfigWithComputed ( )
} else {
config = c . App . ClientConfigWithComputed ( )
}
w . Write ( [ ] byte ( model . MapToJson ( config ) ) )
2017-03-27 09:19:53 -04:00
}
2018-04-09 12:16:11 -04:00
func getEnvironmentConfig ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2018-04-09 12:16:11 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
envConfig := c . App . GetEnvironmentConfig ( )
w . Header ( ) . Set ( "Cache-Control" , "no-cache, no-store, must-revalidate" )
w . Write ( [ ] byte ( model . StringInterfaceToJson ( envConfig ) ) )
}
2017-03-27 09:19:53 -04:00
func getClientLicense ( c * Context , w http . ResponseWriter , r * http . Request ) {
format := r . URL . Query ( ) . Get ( "format" )
if format == "" {
c . Err = model . NewAppError ( "getClientLicense" , "api.license.client.old_format.app_error" , nil , "" , http . StatusNotImplemented )
return
}
if format != "old" {
c . SetInvalidParam ( "format" )
return
}
2018-02-09 10:04:48 -06:00
etag := c . App . GetClientLicenseEtag ( true )
2017-09-22 12:54:27 -05:00
if c . HandleEtag ( etag , "Get Client License" , w , r ) {
2017-03-27 09:19:53 -04:00
return
}
2017-04-27 10:57:58 -04:00
var clientLicense map [ string ] string
2018-11-28 10:56:21 -08:00
if c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2018-02-09 10:04:48 -06:00
clientLicense = c . App . ClientLicense ( )
2017-04-27 10:57:58 -04:00
} else {
2018-02-09 10:04:48 -06:00
clientLicense = c . App . GetSanitizedClientLicense ( )
2017-04-27 10:57:58 -04:00
}
2017-03-27 09:19:53 -04:00
w . Header ( ) . Set ( model . HEADER_ETAG_SERVER , etag )
2017-04-27 10:57:58 -04:00
w . Write ( [ ] byte ( model . MapToJson ( clientLicense ) ) )
2017-03-27 09:19:53 -04:00
}
2017-06-19 13:46:51 -04:00
func addLicense ( c * Context , w http . ResponseWriter , r * http . Request ) {
c . LogAudit ( "attempt" )
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-06-19 13:46:51 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-10-18 15:36:43 -07:00
err := r . ParseMultipartForm ( * c . App . Config ( ) . FileSettings . MaxFileSize )
2017-06-19 13:46:51 -04:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
m := r . MultipartForm
fileArray , ok := m . File [ "license" ]
if ! ok {
c . Err = model . NewAppError ( "addLicense" , "api.license.add_license.no_file.app_error" , nil , "" , http . StatusBadRequest )
return
}
if len ( fileArray ) <= 0 {
c . Err = model . NewAppError ( "addLicense" , "api.license.add_license.array.app_error" , nil , "" , http . StatusBadRequest )
return
}
fileData := fileArray [ 0 ]
file , err := fileData . Open ( )
if err != nil {
c . Err = model . NewAppError ( "addLicense" , "api.license.add_license.open.app_error" , nil , err . Error ( ) , http . StatusBadRequest )
return
}
2017-11-03 10:25:38 -05:00
defer file . Close ( )
2017-06-19 13:46:51 -04:00
buf := bytes . NewBuffer ( nil )
io . Copy ( buf , file )
2018-08-01 16:55:18 +02:00
license , appErr := c . App . SaveLicense ( buf . Bytes ( ) )
if appErr != nil {
if appErr . Id == model . EXPIRED_LICENSE_ERROR {
2017-06-19 13:46:51 -04:00
c . LogAudit ( "failed - expired or non-started license" )
2018-08-01 16:55:18 +02:00
} else if appErr . Id == model . INVALID_LICENSE_ERROR {
2017-06-19 13:46:51 -04:00
c . LogAudit ( "failed - invalid license" )
} else {
c . LogAudit ( "failed - unable to save license" )
}
2018-08-01 16:55:18 +02:00
c . Err = appErr
2017-06-19 13:46:51 -04:00
return
}
2018-08-01 16:55:18 +02:00
c . LogAudit ( "success" )
w . Write ( [ ] byte ( license . ToJson ( ) ) )
2017-06-19 13:46:51 -04:00
}
func removeLicense ( c * Context , w http . ResponseWriter , r * http . Request ) {
c . LogAudit ( "attempt" )
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-06-19 13:46:51 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-06 17:12:54 -05:00
if err := c . App . RemoveLicense ( ) ; err != nil {
2017-06-19 13:46:51 -04:00
c . Err = err
return
}
c . LogAudit ( "success" )
ReturnStatusOK ( w )
}
2017-06-19 16:35:53 -04:00
func getAnalytics ( c * Context , w http . ResponseWriter , r * http . Request ) {
name := r . URL . Query ( ) . Get ( "name" )
teamId := r . URL . Query ( ) . Get ( "team_id" )
if name == "" {
name = "standard"
}
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2017-06-19 16:35:53 -04:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2017-09-06 17:12:54 -05:00
rows , err := c . App . GetAnalytics ( name , teamId )
2017-06-19 16:35:53 -04:00
if err != nil {
c . Err = err
return
}
if rows == nil {
c . SetInvalidParam ( "name" )
return
}
w . Write ( [ ] byte ( rows . ToJson ( ) ) )
}
2018-03-01 00:12:11 +01:00
2018-03-22 06:53:43 -07:00
func getSupportedTimezones ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-12-17 08:51:46 -08:00
supportedTimezones := c . App . Timezones . GetSupported ( )
if supportedTimezones == nil {
supportedTimezones = make ( [ ] string , 0 )
}
2018-03-22 06:53:43 -07:00
2018-12-17 08:51:46 -08:00
b , err := json . Marshal ( supportedTimezones )
if err != nil {
c . Log . Warn ( "Unable to marshal JSON in timezones." , mlog . Err ( err ) )
w . WriteHeader ( http . StatusInternalServerError )
2018-03-22 06:53:43 -07:00
}
2018-12-17 08:51:46 -08:00
w . Write ( b )
2018-03-22 06:53:43 -07:00
}
2018-03-01 00:12:11 +01:00
func testS3 ( c * Context , w http . ResponseWriter , r * http . Request ) {
cfg := model . ConfigFromJson ( r . Body )
if cfg == nil {
cfg = c . App . Config ( )
}
2018-11-28 10:56:21 -08:00
if ! c . App . SessionHasPermissionTo ( c . App . Session , model . PERMISSION_MANAGE_SYSTEM ) {
2018-03-01 00:12:11 +01:00
c . SetPermissionError ( model . PERMISSION_MANAGE_SYSTEM )
return
}
2018-09-20 19:07:03 +02:00
err := filesstore . CheckMandatoryS3Fields ( & cfg . FileSettings )
2018-03-01 00:12:11 +01:00
if err != nil {
c . Err = err
return
}
2019-01-31 08:12:01 -05:00
if * cfg . FileSettings . AmazonS3SecretAccessKey == model . FAKE_SETTING {
2018-03-13 17:26:56 +01:00
cfg . FileSettings . AmazonS3SecretAccessKey = c . App . Config ( ) . FileSettings . AmazonS3SecretAccessKey
}
2018-03-01 00:12:11 +01:00
license := c . App . License ( )
2018-09-20 19:07:03 +02:00
backend , appErr := filesstore . NewFileBackend ( & cfg . FileSettings , license != nil && * license . Features . Compliance )
2018-03-01 00:12:11 +01:00
if appErr == nil {
appErr = backend . TestConnection ( )
}
if appErr != nil {
c . Err = appErr
return
}
ReturnStatusOK ( w )
}
2018-08-24 08:49:31 -04:00
func getRedirectLocation ( c * Context , w http . ResponseWriter , r * http . Request ) {
2018-10-02 09:00:50 +03:00
m := make ( map [ string ] string )
m [ "location" ] = ""
2019-01-27 18:24:46 -08:00
2019-02-12 08:37:54 -05:00
if ! * c . App . Config ( ) . ServiceSettings . EnableLinkPreviews {
2018-10-02 09:00:50 +03:00
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
return
}
2019-01-27 18:24:46 -08:00
2018-08-24 08:49:31 -04:00
url := r . URL . Query ( ) . Get ( "url" )
if len ( url ) == 0 {
c . SetInvalidParam ( "url" )
return
}
2019-02-01 08:13:51 -08:00
if location , ok := redirectLocationDataCache . Get ( url ) ; ok {
2019-01-27 18:24:46 -08:00
m [ "location" ] = location . ( string )
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
return
}
client := c . App . HTTPService . MakeClient ( false )
client . CheckRedirect = func ( req * http . Request , via [ ] * http . Request ) error {
return http . ErrUseLastResponse
2018-08-24 08:49:31 -04:00
}
res , err := client . Head ( url )
if err != nil {
2019-01-27 18:24:46 -08:00
// Cache failures to prevent retries.
redirectLocationDataCache . AddWithExpiresInSecs ( url , "" , 3600 ) // Expires after 1 hour
// Always return a success status and a JSON string to limit information returned to client.
2018-08-24 08:49:31 -04:00
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
return
}
2019-01-27 18:24:46 -08:00
location := res . Header . Get ( "Location" )
redirectLocationDataCache . AddWithExpiresInSecs ( url , location , 3600 ) // Expires after 1 hour
m [ "location" ] = location
2018-08-24 08:49:31 -04:00
w . Write ( [ ] byte ( model . MapToJson ( m ) ) )
return
}