Skip to content

Commit

Permalink
All stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 13, 2024
1 parent 359b34b commit 7c87d18
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 331 deletions.
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/cockroachdb/pebble v1.1.1
github.com/elastic/go-freelru v0.13.0
github.com/gofiber/fiber/v2 v2.52.4
github.com/julienschmidt/httprouter v1.3.0
github.com/kelindar/binary v1.0.19
github.com/rs/zerolog v1.33.0
github.com/tdewolff/parse/v2 v2.7.15
Expand All @@ -36,23 +36,19 @@ require (
github.com/getsentry/sentry-go v0.28.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM=
github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kelindar/binary v1.0.19 h1:DNyQCtKjkLhBh9pnP49OWREddLB0Mho+1U/AOt/Qzxw=
github.com/kelindar/binary v1.0.19/go.mod h1:/twdz8gRLNMffx0U4UOgqm1LywPs6nd9YK2TX52MDh8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand All @@ -65,8 +63,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand All @@ -82,9 +78,6 @@ github.com/prometheus/common v0.54.0 h1:ZlZy0BgJhTwVZUn7dLOkwCZHUkrAqd3WYtcFCWnM
github.com/prometheus/common v0.54.0/go.mod h1:/TQgMJP5CuVYveyT7n/0Ix8yLNNXy9yRSkhnLTHPDIQ=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand All @@ -108,8 +101,6 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
205 changes: 103 additions & 102 deletions handlers/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"instafix/utils"
"instafix/views"
"instafix/views/model"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/julienschmidt/httprouter"
"github.com/valyala/bytebufferpool"
)

Expand All @@ -26,122 +27,122 @@ func mediaidToCode(mediaID int) string {
return shortCode
}

func Embed() fiber.Handler {
return func(c *fiber.Ctx) error {
c.Set("Content-Type", "text/html; charset=utf-8")
viewsData := &model.ViewsData{}
viewsBuf := bytebufferpool.Get()
defer bytebufferpool.Put(viewsBuf)

postID := c.Params("postID")
mediaNum, err := c.ParamsInt("mediaNum", 0)
if err != nil {
viewsData.Description = "Invalid media number"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
}
imgIndex := c.Query("img_index")
func Embed(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
viewsData := &model.ViewsData{}
viewsBuf := bytebufferpool.Get()
defer bytebufferpool.Put(viewsBuf)

var err error
var mediaNum int
urlQuery := r.URL.Query()
postID := ps.ByName("postID")
mediaNumParams := ps.ByName("mediaNum")
if mediaNumParams == "" {
imgIndex := urlQuery.Get("img_index")
if imgIndex != "" {
mediaNum, err = strconv.Atoi(imgIndex)
if err != nil {
viewsData.Description = "Invalid img_index parameter"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
}
mediaNumParams = imgIndex
}

direct, err := strconv.ParseBool(c.Query("direct", "false"))
mediaNum, err = strconv.Atoi(mediaNumParams)
if err != nil {
viewsData.Description = "Invalid direct parameter"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
viewsData.Description = "Invalid img_index parameter"
views.Embed(viewsData, w)
return
}
}

isGallery, err := strconv.ParseBool(c.Query("gallery", "false"))
if err != nil {
viewsData.Description = "Invalid gallery parameter"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
}
direct, err := strconv.ParseBool(urlQuery.Get("direct"))
if err != nil {
viewsData.Description = "Invalid direct parameter"
views.Embed(viewsData, w)
return
}

// Stories use mediaID (int) instead of postID
if strings.Contains(c.Path(), "/stories/") {
mediaID, err := strconv.Atoi(postID)
if err != nil {
viewsData.Description = "Invalid postID"
views.Embed(viewsData, viewsBuf)
c.Send(viewsBuf.Bytes())
return nil
}
postID = mediaidToCode(mediaID)
}
isGallery, err := strconv.ParseBool(urlQuery.Get("gallery"))
if err != nil {
viewsData.Description = "Invalid gallery parameter"
views.Embed(viewsData, w)
return
}

// If User-Agent is not bot, redirect to Instagram
viewsData.Title = "InstaFix"
viewsData.URL = "https://instagram.com" + c.Path()
if !utils.IsBot(c.Request().Header.UserAgent()) {
return c.Redirect(viewsData.URL)
// Stories use mediaID (int) instead of postID
if strings.Contains(r.URL.Path, "/stories/") {
mediaID, err := strconv.Atoi(postID)
if err != nil {
viewsData.Description = "Invalid postID"
views.Embed(viewsData, w)
return
}
postID = mediaidToCode(mediaID)
}

item, err := scraper.GetData(postID)
if err != nil || len(item.Medias) == 0 {
viewsData.Description = "Post might not be available"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
}
// If User-Agent is not bot, redirect to Instagram
viewsData.Title = "InstaFix"
viewsData.URL = "https://instagram.com" + r.URL.Path
if !utils.IsBot(r.Header.Get("User-Agent")) {
w.Header().Set("Location", viewsData.URL)
return
}

if mediaNum > len(item.Medias) {
viewsData.Description = "Media number out of range"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
} else if len(item.Username) == 0 {
viewsData.Description = "Post not found"
views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
}
item, err := scraper.GetData(postID)
if err != nil || len(item.Medias) == 0 {
viewsData.Description = "Post might not be available"
views.Embed(viewsData, w)
return
}

var sb strings.Builder
sb.Grow(32) // 32 bytes should be enough for most cases
if mediaNum > len(item.Medias) {
viewsData.Description = "Media number out of range"
views.Embed(viewsData, w)
return
} else if len(item.Username) == 0 {
viewsData.Description = "Post not found"
views.Embed(viewsData, w)
return
}

viewsData.Title = "@" + item.Username
// Gallery do not have any caption
if !isGallery {
viewsData.Description = item.Caption
if len(viewsData.Description) > 255 {
viewsData.Description = utils.Substr(viewsData.Description, 0, 250) + "..."
}
}
var sb strings.Builder
sb.Grow(32) // 32 bytes should be enough for most cases

typename := item.Medias[max(1, mediaNum)-1].TypeName
isImage := strings.Contains(typename, "Image") || strings.Contains(typename, "StoryVideo")
switch {
case mediaNum == 0 && isImage && len(item.Medias) > 1:
viewsData.Card = "summary_large_image"
sb.WriteString("/grid/")
sb.WriteString(postID)
viewsData.ImageURL = sb.String()
case isImage:
viewsData.Card = "summary_large_image"
sb.WriteString("/images/")
sb.WriteString(postID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.ImageURL = sb.String()
default:
viewsData.Card = "player"
sb.WriteString("/videos/")
sb.WriteString(postID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.VideoURL = sb.String()
viewsData.OEmbedURL = c.BaseURL() + "/oembed?text=" + url.QueryEscape(viewsData.Description) + "&url=" + viewsData.URL
viewsData.Title = "@" + item.Username
// Gallery do not have any caption
if !isGallery {
viewsData.Description = item.Caption
if len(viewsData.Description) > 255 {
viewsData.Description = utils.Substr(viewsData.Description, 0, 250) + "..."
}
}

if direct {
return c.Redirect(sb.String())
}
typename := item.Medias[max(1, mediaNum)-1].TypeName
isImage := strings.Contains(typename, "Image") || strings.Contains(typename, "StoryVideo")
switch {
case mediaNum == 0 && isImage && len(item.Medias) > 1:
viewsData.Card = "summary_large_image"
sb.WriteString("/grid/")
sb.WriteString(postID)
viewsData.ImageURL = sb.String()
case isImage:
viewsData.Card = "summary_large_image"
sb.WriteString("/images/")
sb.WriteString(postID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.ImageURL = sb.String()
default:
viewsData.Card = "player"
sb.WriteString("/videos/")
sb.WriteString(postID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.VideoURL = sb.String()
viewsData.OEmbedURL = r.Host + "/oembed?text=" + url.QueryEscape(viewsData.Description) + "&url=" + viewsData.URL
}

views.Embed(viewsData, viewsBuf)
return c.Send(viewsBuf.Bytes())
if direct {
w.Header().Set("Location", sb.String())
return
}

views.Embed(viewsData, w)
return
}
Loading

0 comments on commit 7c87d18

Please sign in to comment.