-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
395ade1
commit 2ac7992
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package rest | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/likecoin/likecoin-chain-tx-indexer/logger" | ||
) | ||
|
||
type copyWriter struct { | ||
gin.ResponseWriter | ||
body *bytes.Buffer | ||
} | ||
|
||
func (cw copyWriter) Write(b []byte) (int, error) { | ||
return cw.body.Write(b) | ||
} | ||
|
||
func (cw *copyWriter) WriteHeader(statusCode int) { | ||
// Remove content-length header written by proxy | ||
cw.Header().Del("Content-length") | ||
cw.Header().Set("Transfer-Encoding", "chunked") | ||
cw.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
|
||
func filterContentFingerprints() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
isBrowser := false | ||
switch c.NegotiateFormat(gin.MIMEJSON, gin.MIMEHTML) { | ||
case gin.MIMEHTML: | ||
isBrowser = true | ||
case gin.MIMEJSON: | ||
isBrowser = false | ||
} | ||
|
||
if !isBrowser { | ||
c.Next() | ||
return | ||
} | ||
|
||
wb := ©Writer{ | ||
body: &bytes.Buffer{}, | ||
ResponseWriter: c.Writer, | ||
} | ||
c.Writer = wb | ||
c.Next() | ||
|
||
originBody := wb.body | ||
originBodyBytes := originBody.Bytes() | ||
|
||
var jsonObject map[string]any | ||
err := json.Unmarshal(originBodyBytes, &jsonObject) | ||
if err != nil { | ||
_, e := wb.ResponseWriter.Write(originBodyBytes) | ||
if e != nil { | ||
logger.L.Error(e) | ||
} | ||
return | ||
} | ||
|
||
records, ok := jsonObject["records"].([]interface{}) | ||
if !ok { | ||
_, e := wb.ResponseWriter.Write(originBodyBytes) | ||
if e != nil { | ||
logger.L.Error(e) | ||
} | ||
return | ||
} | ||
|
||
for index, record := range records { | ||
obj, ok := record.(map[string]any) | ||
if !ok { | ||
continue | ||
} | ||
data, ok := obj["data"].(map[string]any) | ||
if !ok { | ||
continue | ||
} | ||
if data["contentFingerprints"] != nil { | ||
delete(data, "contentFingerprints") | ||
} | ||
if data["contentMetadata"] != nil { | ||
contentMetadata, ok := data["contentMetadata"].(map[string]any) | ||
if ok { | ||
if contentMetadata["sameAs"] != nil { | ||
delete(contentMetadata, "sameAs") | ||
} | ||
data["contentMetadata"] = contentMetadata | ||
} | ||
} | ||
obj["data"] = data | ||
records[index] = obj | ||
} | ||
jsonObject["records"] = records | ||
|
||
newBody, err := json.Marshal(jsonObject) | ||
if err != nil { | ||
_, e := wb.ResponseWriter.Write(originBodyBytes) | ||
if e != nil { | ||
logger.L.Error(e) | ||
} | ||
return | ||
} | ||
_, e := wb.ResponseWriter.Write(newBody) | ||
if e != nil { | ||
logger.L.Error(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters