mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Implemented initial changes for files table * Removed *_benchmark_test.go files * Re-implemented GetPublicFile and added support for old path * Localization for files table * Moved file system code into utils package * Finished server-side changes and added initial upgrade script * Added getPostFiles api * Re-add Extension and HasPreviewImage fields to FileInfo * Removed unused translation * Fixed merge conflicts left over after permissions changes * Forced FileInfo.extension to be lower case * Changed FileUploadResponse to contain the FileInfos instead of FileIds * Fixed permissions on getFile* calls * Fixed notifications for file uploads * Added initial version of client code for files changes * Permanently added FileIds field to Post object and removed Post.HasFiles * Updated PostStore.Update to be usable in more circumstances * Re-added Filenames field and switched file migration to be entirely lazy-loaded * Increased max listener count for FileStore * Removed unused fileInfoCache * Moved file system code back into api * Removed duplicate test case * Fixed unit test running on ports other than 8065 * Renamed HasPermissionToPostContext to HasPermissionToChannelByPostContext * Refactored handleImages to make it more easily understandable * Renamed getPostFiles to getFileInfosForPost * Re-added pre-FileIds posts to analytics * Changed files to be saved as their ids as opposed to id/filename.ext * Renamed FileInfo.UserId to FileInfo.CreatorId * Fixed detection of language in CodePreview * Fixed switching between threads in the RHS not loading new files * Add serverside protection against a rare bug where the client sends the same file twice for a single post * Refactored the important parts of uploadFile api call into a function that can be called without a web context
131 lines
4.5 KiB
Go
131 lines
4.5 KiB
Go
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/mattermost/platform/model"
|
|
"github.com/mattermost/platform/utils"
|
|
|
|
_ "github.com/nicksnyder/go-i18n/i18n"
|
|
)
|
|
|
|
type Routes struct {
|
|
Root *mux.Router // ''
|
|
ApiRoot *mux.Router // 'api/v3'
|
|
|
|
Users *mux.Router // 'api/v3/users'
|
|
NeedUser *mux.Router // 'api/v3/users/{user_id:[A-Za-z0-9]+}'
|
|
|
|
Teams *mux.Router // 'api/v3/teams'
|
|
NeedTeam *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}'
|
|
|
|
Channels *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels'
|
|
NeedChannel *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}'
|
|
NeedChannelName *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}'
|
|
|
|
Posts *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}/posts'
|
|
NeedPost *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}'
|
|
|
|
Commands *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/commands'
|
|
Hooks *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/hooks'
|
|
|
|
TeamFiles *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/files'
|
|
Files *mux.Router // 'api/v3/files'
|
|
NeedFile *mux.Router // 'api/v3/files/{file_id:[A-Za-z0-9]+}'
|
|
|
|
OAuth *mux.Router // 'api/v3/oauth'
|
|
|
|
Admin *mux.Router // 'api/v3/admin'
|
|
|
|
General *mux.Router // 'api/v3/general'
|
|
|
|
Preferences *mux.Router // 'api/v3/preferences'
|
|
|
|
License *mux.Router // 'api/v3/license'
|
|
|
|
Public *mux.Router // 'api/v3/public'
|
|
|
|
Emoji *mux.Router // 'api/v3/emoji'
|
|
|
|
Webrtc *mux.Router // 'api/v3/webrtc'
|
|
|
|
WebSocket *WebSocketRouter // websocket api
|
|
}
|
|
|
|
var BaseRoutes *Routes
|
|
|
|
func InitApi() {
|
|
BaseRoutes = &Routes{}
|
|
BaseRoutes.Root = Srv.Router
|
|
BaseRoutes.ApiRoot = Srv.Router.PathPrefix(model.API_URL_SUFFIX).Subrouter()
|
|
BaseRoutes.Users = BaseRoutes.ApiRoot.PathPrefix("/users").Subrouter()
|
|
BaseRoutes.NeedUser = BaseRoutes.Users.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
|
|
BaseRoutes.Teams = BaseRoutes.ApiRoot.PathPrefix("/teams").Subrouter()
|
|
BaseRoutes.NeedTeam = BaseRoutes.Teams.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter()
|
|
BaseRoutes.Channels = BaseRoutes.NeedTeam.PathPrefix("/channels").Subrouter()
|
|
BaseRoutes.NeedChannel = BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter()
|
|
BaseRoutes.NeedChannelName = BaseRoutes.Channels.PathPrefix("/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
|
|
BaseRoutes.Posts = BaseRoutes.NeedChannel.PathPrefix("/posts").Subrouter()
|
|
BaseRoutes.NeedPost = BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
|
|
BaseRoutes.Commands = BaseRoutes.NeedTeam.PathPrefix("/commands").Subrouter()
|
|
BaseRoutes.TeamFiles = BaseRoutes.NeedTeam.PathPrefix("/files").Subrouter()
|
|
BaseRoutes.Files = BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter()
|
|
BaseRoutes.NeedFile = BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter()
|
|
BaseRoutes.Hooks = BaseRoutes.NeedTeam.PathPrefix("/hooks").Subrouter()
|
|
BaseRoutes.OAuth = BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter()
|
|
BaseRoutes.Admin = BaseRoutes.ApiRoot.PathPrefix("/admin").Subrouter()
|
|
BaseRoutes.General = BaseRoutes.ApiRoot.PathPrefix("/general").Subrouter()
|
|
BaseRoutes.Preferences = BaseRoutes.ApiRoot.PathPrefix("/preferences").Subrouter()
|
|
BaseRoutes.License = BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
|
|
BaseRoutes.Public = BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter()
|
|
BaseRoutes.Emoji = BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
|
|
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
|
|
|
|
BaseRoutes.WebSocket = NewWebSocketRouter()
|
|
|
|
InitUser()
|
|
InitTeam()
|
|
InitChannel()
|
|
InitPost()
|
|
InitWebSocket()
|
|
InitFile()
|
|
InitCommand()
|
|
InitAdmin()
|
|
InitGeneral()
|
|
InitOAuth()
|
|
InitWebhook()
|
|
InitPreference()
|
|
InitLicense()
|
|
InitEmoji()
|
|
InitStatus()
|
|
InitWebrtc()
|
|
|
|
// 404 on any api route before web.go has a chance to serve it
|
|
Srv.Router.Handle("/api/{anything:.*}", http.HandlerFunc(Handle404))
|
|
|
|
utils.InitHTML()
|
|
|
|
InitEmailBatching()
|
|
}
|
|
|
|
func HandleEtag(etag string, w http.ResponseWriter, r *http.Request) bool {
|
|
if et := r.Header.Get(model.HEADER_ETAG_CLIENT); len(etag) > 0 {
|
|
if et == etag {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func ReturnStatusOK(w http.ResponseWriter) {
|
|
m := make(map[string]string)
|
|
m[model.STATUS] = model.STATUS_OK
|
|
w.Write([]byte(model.MapToJson(m)))
|
|
}
|