diff --git a/src/github.com/turt2live/matrix-media-repo/client/r0/download.go b/src/github.com/turt2live/matrix-media-repo/client/r0/download.go index 928cc315b991e7450fb89bd974ea4b2880fa43f1..6c06e5c4be6ae5a6855a92505eb8e10687341101 100644 --- a/src/github.com/turt2live/matrix-media-repo/client/r0/download.go +++ b/src/github.com/turt2live/matrix-media-repo/client/r0/download.go @@ -12,19 +12,11 @@ import ( "github.com/turt2live/matrix-media-repo/util" ) -// Request: -// Path params: {serverName}, {mediaId} -// Optional path param: {filename} -// -// Response: -// Headers: Content-Type, Content-Disposition -// Body: <byte[]> - type DownloadMediaResponse struct { ContentType string - Filename string - SizeBytes int64 - Location string + Filename string + SizeBytes int64 + Location string } func DownloadMedia(w http.ResponseWriter, r *http.Request, db storage.Database, c config.MediaRepoConfig, log *logrus.Entry) interface{} { @@ -39,8 +31,8 @@ func DownloadMedia(w http.ResponseWriter, r *http.Request, db storage.Database, filename := params["filename"] log = log.WithFields(logrus.Fields{ - "mediaId": mediaId, - "server": server, + "mediaId": mediaId, + "server": server, "filename": filename, }) @@ -76,4 +68,4 @@ func ValidateUserCanDownload(r *http.Request, db storage.Database, c config.Medi accessToken := util.GetAccessTokenFromRequest(r) userId, err := util.GetUserIdFromToken(r.Context(), r.Host, accessToken, c) return userId != "" && err != nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/client/r0/thumbnail.go b/src/github.com/turt2live/matrix-media-repo/client/r0/thumbnail.go index b1830d255af2d312f78a96b47183a90774ad567d..1b1b16068966569ffea903bd4903c4f835ea7a55 100644 --- a/src/github.com/turt2live/matrix-media-repo/client/r0/thumbnail.go +++ b/src/github.com/turt2live/matrix-media-repo/client/r0/thumbnail.go @@ -12,17 +12,6 @@ import ( "github.com/turt2live/matrix-media-repo/storage" ) - - -// Request: -// Path params: {serverName}, {mediaId} -// QS: ?width=&height=&method= -// "method" can be crop or scale -// -// Response: -// Headers: Content-Type -// Body: <byte[]> - func ThumbnailMedia(w http.ResponseWriter, r *http.Request, db storage.Database, c config.MediaRepoConfig, log *logrus.Entry) interface{} { if !ValidateUserCanDownload(r, db, c) { return client.AuthFailed() @@ -35,7 +24,7 @@ func ThumbnailMedia(w http.ResponseWriter, r *http.Request, db storage.Database, log = log.WithFields(logrus.Fields{ "mediaId": mediaId, - "server": server, + "server": server, }) widthStr := r.URL.Query().Get("width") diff --git a/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go b/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go index 34904c2fc3fb30d3f0b8aafce385d7924928950d..919a34050302ed5fe6985c04999dccf20a31b620 100644 --- a/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go +++ b/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go @@ -12,14 +12,6 @@ import ( "github.com/turt2live/matrix-media-repo/util" ) -// Request: -// QS: ?filename= -// Headers: Content-Type -// Body: <byte[]> -// -// Response: -// Body: {"content_uri":"mxc://domain.com/media_id"} - type MediaUploadedResponse struct { ContentUri string `json:"content_uri"` } @@ -38,7 +30,7 @@ func UploadMedia(w http.ResponseWriter, r *http.Request, db storage.Database, c log = log.WithFields(logrus.Fields{ "filename": filename, - "userId": userId, + "userId": userId, }) contentType := r.Header.Get("Content-Type") @@ -67,4 +59,4 @@ func UploadMedia(w http.ResponseWriter, r *http.Request, db storage.Database, c } return &MediaUploadedResponse{mxc} -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/client/responses.go b/src/github.com/turt2live/matrix-media-repo/client/responses.go index 5b0a3c67eb031394d4687bca240c46b0e0984e4c..dce0f9b39dc925c89debc1821cb51e174d2ed6ac 100644 --- a/src/github.com/turt2live/matrix-media-repo/client/responses.go +++ b/src/github.com/turt2live/matrix-media-repo/client/responses.go @@ -1,8 +1,8 @@ package client type ErrorResponse struct { - Code string `json:"errcode"` - Message string `json:"error"` + Code string `json:"errcode"` + Message string `json:"error"` InternalCode string `json:"mr_errcode"` } @@ -20,4 +20,4 @@ func RequestTooLarge() *ErrorResponse { func AuthFailed() *ErrorResponse { return &ErrorResponse{"M_UNKNOWN_TOKEN", "Authentication Failed", "M_UNKNOWN_TOKEN"} -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go b/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go index 8111e03984ddefa54ee109a7a451cc85bdde1726..62ce55ea8b14491264f76b433f49d50127406dee 100644 --- a/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go +++ b/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go @@ -26,17 +26,17 @@ type requestCounter struct { } type Handler struct { - h func(http.ResponseWriter, *http.Request, storage.Database, config.MediaRepoConfig, *log.Entry) interface{} + h func(http.ResponseWriter, *http.Request, storage.Database, config.MediaRepoConfig, *log.Entry) interface{} opts HandlerOpts } type HandlerOpts struct { - db storage.Database - config config.MediaRepoConfig + db storage.Database + config config.MediaRepoConfig reqCounter *requestCounter } -type EmptyResponse struct {} +type EmptyResponse struct{} func main() { rtr := mux.NewRouter() @@ -82,7 +82,7 @@ func main() { // TODO: Intercept 404, 500, and 400 to respond with M_NOT_FOUND and M_UNKNOWN // TODO: Rate limiting (429 M_LIMIT_EXCEEDED) - address:=c.General.BindAddress+":"+strconv.Itoa(c.General.Port) + address := c.General.BindAddress + ":" + strconv.Itoa(c.General.Port) http.Handle("/", rtr) log.WithField("address", address).Info("Started up. Listening at http://" + address) @@ -91,13 +91,13 @@ func main() { func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { contextLog := log.WithFields(log.Fields{ - "method": r.Method, - "host": r.Host, - "resource": r.URL.Path, - "contentType": r.Header.Get("Content-Type"), + "method": r.Method, + "host": r.Host, + "resource": r.URL.Path, + "contentType": r.Header.Get("Content-Type"), "contentLength": r.Header.Get("Content-Length"), - "queryString": util.GetLogSafeQueryString(r), - "requestId": h.opts.reqCounter.GetNextId(), + "queryString": util.GetLogSafeQueryString(r), + "requestId": h.opts.reqCounter.GetNextId(), }) contextLog.Info("Received request") @@ -127,7 +127,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } jsonStr := string(b) - contextLog.Info("Replying with result: " + reflect.TypeOf(res).Elem().Name() +" " + jsonStr) + contextLog.Info("Replying with result: " + reflect.TypeOf(res).Elem().Name() + " " + jsonStr) switch result := res.(type) { case *client.ErrorResponse: @@ -142,7 +142,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "M_MEDIA_TOO_LARGE": http.Error(w, jsonStr, http.StatusRequestEntityTooLarge) break - //case "M_UNKNOWN": + //case "M_UNKNOWN": default: http.Error(w, jsonStr, http.StatusInternalServerError) break @@ -150,7 +150,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { break case *r0.DownloadMediaResponse: w.Header().Set("Content-Type", result.ContentType) - w.Header().Set("Content-Disposition", "inline; filename=\"" + result.Filename +"\"") + w.Header().Set("Content-Disposition", "inline; filename=\""+result.Filename+"\"") w.Header().Set("Content-Length", fmt.Sprint(result.SizeBytes)) f, err := os.Open(result.Location) if err != nil { @@ -173,4 +173,4 @@ func (c *requestCounter) GetNextId() string { c.lastId = c.lastId + 1 return "REQ-" + strId -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/config/config.go b/src/github.com/turt2live/matrix-media-repo/config/config.go index 9037d51d7334d3a67dbc21301b01627804324d79..4b39c5e0aecf6b4a96b31dd1d63db1f763578ab7 100644 --- a/src/github.com/turt2live/matrix-media-repo/config/config.go +++ b/src/github.com/turt2live/matrix-media-repo/config/config.go @@ -8,15 +8,15 @@ import ( ) type HomeserverConfig struct { - Name string `yaml:"name"` - DownloadRequiresAuth bool `yaml:"downloadRequiresAuth"` - ClientServerApi string `yaml:"csApi"` + Name string `yaml:"name"` + DownloadRequiresAuth bool `yaml:"downloadRequiresAuth"` + ClientServerApi string `yaml:"csApi"` } type MediaRepoConfig struct { General struct { - BindAddress string `yaml:"bindAddress"` - Port int `yaml:"port"` + BindAddress string `yaml:"bindAddress"` + Port int `yaml:"port"` LogDirectory string `yaml:"logDirectory"` } `yaml:"repo"` @@ -28,7 +28,7 @@ type MediaRepoConfig struct { Uploads struct { StoragePaths []string `yaml:"storagePaths,flow"` - MaxSizeBytes int64 `yaml:"maxBytes"` + MaxSizeBytes int64 `yaml:"maxBytes"` } `yaml:"uploads"` Downloads struct { @@ -38,8 +38,8 @@ type MediaRepoConfig struct { Thumbnails struct { MaxSourceBytes int64 `yaml:"maxSourceBytes"` Sizes []struct { - Width int `yaml:"width"` - Height int `yaml:"height"` + Width int `yaml:"width"` + Height int `yaml:"height"` Method string `yaml:"method"` } `yaml:"sizes,flow"` } `yaml:"thumbnails"` @@ -62,4 +62,4 @@ func ReadConfig() (MediaRepoConfig, error) { } return *c, nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/logging/logger.go b/src/github.com/turt2live/matrix-media-repo/logging/logger.go index ca434f4e9517b4c756b6de851123d26074e8deae..9c92c9c2a4208f808879ed9ae4bf80e3e93846dd 100644 --- a/src/github.com/turt2live/matrix-media-repo/logging/logger.go +++ b/src/github.com/turt2live/matrix-media-repo/logging/logger.go @@ -31,15 +31,17 @@ func Setup(dir string) error { }) logrus.SetOutput(os.Stdout) - if dir == "" { return nil } + if dir == "" { + return nil + } _ = os.MkdirAll(dir, os.ModePerm) logFile := path.Join(dir, "media_repo.log") writer, err := rotatelogs.New( - logFile + ".%Y%m%d%H%M", + logFile+".%Y%m%d%H%M", rotatelogs.WithLinkName(logFile), - rotatelogs.WithMaxAge((24 * time.Hour) * 14), // keep for 14 days - rotatelogs.WithRotationTime(24 * time.Hour), // rotate every 24 hours + rotatelogs.WithMaxAge((24*time.Hour)*14), // keep for 14 days + rotatelogs.WithRotationTime(24*time.Hour), // rotate every 24 hours ) if err != nil { return err @@ -47,12 +49,12 @@ func Setup(dir string) error { logrus.AddHook(lfshook.NewHook(lfshook.WriterMap{ logrus.DebugLevel: writer, - logrus.InfoLevel: writer, - logrus.WarnLevel: writer, + logrus.InfoLevel: writer, + logrus.WarnLevel: writer, logrus.ErrorLevel: writer, logrus.FatalLevel: writer, logrus.PanicLevel: writer, })) return nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/media_handler/locator.go b/src/github.com/turt2live/matrix-media-repo/media_handler/locator.go index 3bfff80c97bee85f819a73f5983e9c3a6c0a3267..8656bd39667b5bfe418d7ceba360696dfcf9f751 100644 --- a/src/github.com/turt2live/matrix-media-repo/media_handler/locator.go +++ b/src/github.com/turt2live/matrix-media-repo/media_handler/locator.go @@ -82,4 +82,4 @@ func DownloadMedia(ctx context.Context, server string, mediaId string, c config. log.Info("Persisting downloaded remote media") return request.StoreMediaWithId(ctx, mediaId, c, db, log) -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/media_handler/media_handler.go b/src/github.com/turt2live/matrix-media-repo/media_handler/media_handler.go index baa97c0be6ed26b59bb79eeffc2dc30c45667b11..ad480ada2e531b015979df40e4dcf1ec72f1f9eb 100644 --- a/src/github.com/turt2live/matrix-media-repo/media_handler/media_handler.go +++ b/src/github.com/turt2live/matrix-media-repo/media_handler/media_handler.go @@ -14,11 +14,11 @@ import ( ) type MediaUploadRequest struct { - Contents io.Reader + Contents io.Reader DesiredFilename string - UploadedBy string - Host string - ContentType string + UploadedBy string + Host string + ContentType string } func (r MediaUploadRequest) StoreAndGetMxcUri(ctx context.Context, c config.MediaRepoConfig, db storage.Database, log *logrus.Entry) (string, error) { @@ -139,4 +139,4 @@ func IsMediaSame(media types.Media, r MediaUploadRequest) bool { typeSame := media.ContentType == r.ContentType return originSame && nameSame && userSame && typeSame -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go b/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go index a93a6a1652644675c6a0f2d1f2380f4f3178bd92..02f15351937b7ab5cd5575bd4c48cd23e27de924 100644 --- a/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go +++ b/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go @@ -32,7 +32,7 @@ func GetThumbnail(ctx context.Context, media types.Media, width int, height int, for i := 0; i < len(c.Thumbnails.Sizes); i++ { size := c.Thumbnails.Sizes[i] - lastSize := i == len(c.Thumbnails.Sizes) - 1 + lastSize := i == len(c.Thumbnails.Sizes)-1 if width == size.Width && height == size.Height { targetWidth = width @@ -60,7 +60,7 @@ func GetThumbnail(ctx context.Context, media types.Media, width int, height int, } log = log.WithFields(logrus.Fields{ - "targetWidth": targetWidth, + "targetWidth": targetWidth, "targetHeight": targetHeight, }) log.Info("Looking up thumbnail") @@ -163,4 +163,4 @@ func generateThumbnail(ctx context.Context, media types.Media, width int, height } return *thumb, nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/storage/filestore.go b/src/github.com/turt2live/matrix-media-repo/storage/filestore.go index edc5a714b0fbae49a7ccf039c2a1ce0530a55173..e5080645999fcfd1fd1beeff4758643045a93e2b 100644 --- a/src/github.com/turt2live/matrix-media-repo/storage/filestore.go +++ b/src/github.com/turt2live/matrix-media-repo/storage/filestore.go @@ -93,4 +93,4 @@ func GetFileHash(filePath string) (string, error) { defer f.Close() return hex.EncodeToString(hasher.Sum(nil)), nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/storage/schema/media.go b/src/github.com/turt2live/matrix-media-repo/storage/schema/media.go index 15e10731f51a404e230e7278d3784e38abd371d3..02db5a33f8eb9e48ca51df1a10f81edde299b2c7 100644 --- a/src/github.com/turt2live/matrix-media-repo/storage/schema/media.go +++ b/src/github.com/turt2live/matrix-media-repo/storage/schema/media.go @@ -24,4 +24,4 @@ func PrepareMedia(db *sql.DB) (err error) { } return -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/storage/schema/thumbnail.go b/src/github.com/turt2live/matrix-media-repo/storage/schema/thumbnail.go index c3fe68a0b0b42b4ae5f1cec4a5f53b4b4ae7daed..08da89c94191caad2dd5db0098601cd901979e84 100644 --- a/src/github.com/turt2live/matrix-media-repo/storage/schema/thumbnail.go +++ b/src/github.com/turt2live/matrix-media-repo/storage/schema/thumbnail.go @@ -24,4 +24,4 @@ func PrepareThumbnails(db *sql.DB) (err error) { } return -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/storage/storage.go b/src/github.com/turt2live/matrix-media-repo/storage/storage.go index 17976a8072ba2693bd3ace607145bbd0e136f3a7..c421b86eec69af2df0026834babd72f7dc1a292c 100644 --- a/src/github.com/turt2live/matrix-media-repo/storage/storage.go +++ b/src/github.com/turt2live/matrix-media-repo/storage/storage.go @@ -26,12 +26,12 @@ type Database struct { } type statements struct { - selectMedia *sql.Stmt - selectMediaByHash *sql.Stmt - insertMedia *sql.Stmt + selectMedia *sql.Stmt + selectMediaByHash *sql.Stmt + insertMedia *sql.Stmt selectSizeOfFolder *sql.Stmt - selectThumbnail *sql.Stmt - insertThumbnail *sql.Stmt + selectThumbnail *sql.Stmt + insertThumbnail *sql.Stmt } func OpenDatabase(connectionString string) (*Database, error) { @@ -46,12 +46,24 @@ func OpenDatabase(connectionString string) (*Database, error) { schema.PrepareThumbnails(d.db) // prepare a bunch of statements for use - if d.statements.selectMedia, err = d.db.Prepare(selectMedia); err != nil { return nil, err } - if d.statements.selectMediaByHash, err = d.db.Prepare(selectMediaByHash); err != nil { return nil, err } - if d.statements.insertMedia, err = d.db.Prepare(insertMedia); err != nil { return nil, err } - if d.statements.selectSizeOfFolder, err = d.db.Prepare(selectSizeOfFolder); err != nil { return nil, err } - if d.statements.selectThumbnail, err = d.db.Prepare(selectThumbnail); err != nil { return nil, err } - if d.statements.insertThumbnail, err = d.db.Prepare(insertThumbnail); err != nil { return nil, err } + if d.statements.selectMedia, err = d.db.Prepare(selectMedia); err != nil { + return nil, err + } + if d.statements.selectMediaByHash, err = d.db.Prepare(selectMediaByHash); err != nil { + return nil, err + } + if d.statements.insertMedia, err = d.db.Prepare(insertMedia); err != nil { + return nil, err + } + if d.statements.selectSizeOfFolder, err = d.db.Prepare(selectSizeOfFolder); err != nil { + return nil, err + } + if d.statements.selectThumbnail, err = d.db.Prepare(selectThumbnail); err != nil { + return nil, err + } + if d.statements.insertThumbnail, err = d.db.Prepare(insertThumbnail); err != nil { + return nil, err + } return &d, nil } @@ -155,4 +167,4 @@ func (d *Database) GetThumbnail(ctx context.Context, origin string, mediaId stri &t.CreationTs, ) return *t, err -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/types/media.go b/src/github.com/turt2live/matrix-media-repo/types/media.go index 73b0b824aff9699236dd5b308ffd1e155533cae1..99789bc4ac19905caf2dd8ab1cf3b32f53c0e954 100644 --- a/src/github.com/turt2live/matrix-media-repo/types/media.go +++ b/src/github.com/turt2live/matrix-media-repo/types/media.go @@ -1,13 +1,13 @@ package types type Media struct { - Origin string - MediaId string - UploadName string + Origin string + MediaId string + UploadName string ContentType string - UserId string - Sha256Hash string - SizeBytes int64 - Location string - CreationTs int64 + UserId string + Sha256Hash string + SizeBytes int64 + Location string + CreationTs int64 } diff --git a/src/github.com/turt2live/matrix-media-repo/types/thumbnail.go b/src/github.com/turt2live/matrix-media-repo/types/thumbnail.go index 52a6e915604715b8f317a15aa057909d1eacb27e..a296b38861e632cbc2fd20a570cbd1cab512eb24 100644 --- a/src/github.com/turt2live/matrix-media-repo/types/thumbnail.go +++ b/src/github.com/turt2live/matrix-media-repo/types/thumbnail.go @@ -1,13 +1,13 @@ package types type Thumbnail struct { - Origin string - MediaId string - Width int - Height int - Method string // "crop" or "scale" + Origin string + MediaId string + Width int + Height int + Method string // "crop" or "scale" ContentType string - SizeBytes int64 - Location string - CreationTs int64 + SizeBytes int64 + Location string + CreationTs int64 } diff --git a/src/github.com/turt2live/matrix-media-repo/util/config.go b/src/github.com/turt2live/matrix-media-repo/util/config.go index feeab34037db5db8bf0e8c8cfb3bc67206859fe7..d53f80396de3a30b7da6667d0be1e9b796a782be 100644 --- a/src/github.com/turt2live/matrix-media-repo/util/config.go +++ b/src/github.com/turt2live/matrix-media-repo/util/config.go @@ -16,4 +16,4 @@ func GetHomeserverConfig(server string, c config.MediaRepoConfig) (*config.Homes } return nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/util/files.go b/src/github.com/turt2live/matrix-media-repo/util/files.go index 598644f6649bfd124b48685970a840cf0085a160..11450f92d7ec91eef98f5b6ee6120a8741ae5fc9 100644 --- a/src/github.com/turt2live/matrix-media-repo/util/files.go +++ b/src/github.com/turt2live/matrix-media-repo/util/files.go @@ -4,8 +4,12 @@ import "os" func FileExists(path string) (bool, error) { _, err := os.Stat(path) - if err == nil { return true, nil } - if os.IsNotExist(err) { return false, nil } + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } return true, err } @@ -23,4 +27,4 @@ func FileSize(path string) (int64, error) { } return fi.Size(), nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/util/matrix.go b/src/github.com/turt2live/matrix-media-repo/util/matrix.go index fc47f26c7d39717659c224458f9f05fddcb1dd6e..ce4e49b2bb0fb17c51769d1c33530382c456a6bd 100644 --- a/src/github.com/turt2live/matrix-media-repo/util/matrix.go +++ b/src/github.com/turt2live/matrix-media-repo/util/matrix.go @@ -26,4 +26,4 @@ func GetUserIdFromToken(ctx context.Context, serverName string, accessToken stri } return response.UserId, nil -} \ No newline at end of file +} diff --git a/src/github.com/turt2live/matrix-media-repo/util/random.go b/src/github.com/turt2live/matrix-media-repo/util/random.go index a841a98d24bf392dbe277c67dfa00d1db704bfff..e2cb48a7c1be05ce60cc0e5d844fec8daea84020 100644 --- a/src/github.com/turt2live/matrix-media-repo/util/random.go +++ b/src/github.com/turt2live/matrix-media-repo/util/random.go @@ -24,4 +24,4 @@ func GenerateRandomString(nBytes int) (string, error) { hasher := md5.New() hasher.Write(b) return hex.EncodeToString(hasher.Sum(nil)), nil -} \ No newline at end of file +}