package httpRequest import ( "io" "net/http" "strings" "go-socket/packages/config" ) type bodyLimit uint8 const ( normal bodyLimit = iota file avatar profileBg hubIcon hubBackground ) func validCheckWithResponseOnFail(response http.ResponseWriter, request *http.Request, pt bodyLimit) bool { var maxSize int64 switch pt { case file: maxSize = int64(config.MaxRequestWithFileBytes) break case avatar: maxSize = int64(config.MaxRequestWithAvatarBytes) break case profileBg: maxSize = int64(config.MaxRequestWithProfileBgBytes) break case hubIcon: maxSize = int64(config.MaxRequestWithHubIconBytes) break case hubBackground: maxSize = int64(config.MaxRequestWithHubBackgroundBytes) break default: maxSize = int64(config.MaxRequestBytes) } request.Body = http.MaxBytesReader(response, request.Body, maxSize) if request.ContentLength > maxSize { io.Copy(io.Discard, request.Body) http.Error(response, "Request too large", http.StatusRequestEntityTooLarge) return false } return true } func isImage(r io.Reader) (bool, string, error) { buf := make([]byte, 512) n, err := io.ReadFull(r, buf) if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { return false, "", err } contentType := http.DetectContentType(buf[:n]) isImage := strings.HasPrefix(contentType, "image/") && contentType != "image/svg+xml" if seeker, ok := r.(io.Seeker); ok { if _, err := seeker.Seek(0, io.SeekStart); err != nil { return isImage, contentType, err } } return isImage, contentType, nil }