Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Nov 1, 2024
1 parent e12950e commit 4c79c0e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
28 changes: 20 additions & 8 deletions backend/routes/indexer/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,36 @@ func processNFTMintedEvent(event IndexerEvent) {
}
}

if _, err := os.Stat("nfts/images"); os.IsNotExist(err) {
err = os.MkdirAll("nfts/images", os.ModePerm)
roundDir := fmt.Sprintf("nfts/round-%d", dayIndex/7+1)
if _, err := os.Stat(roundDir); os.IsNotExist(err) {
err = os.MkdirAll(roundDir, os.ModePerm)
if err != nil {
PrintIndexerError("processNFTMintedEvent", "Error creating nfts/images directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
PrintIndexerError("processNFTMintedEvent", "Error creating round directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
return
}
}

if _, err := os.Stat("nfts/meta"); os.IsNotExist(err) {
err = os.MkdirAll("nfts/meta", os.ModePerm)
imagesDir := fmt.Sprintf("%s/images", roundDir)
if _, err := os.Stat(imagesDir); os.IsNotExist(err) {
err = os.MkdirAll(imagesDir, os.ModePerm)
if err != nil {
PrintIndexerError("processNFTMintedEvent", "Error creating nfts/meta directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
PrintIndexerError("processNFTMintedEvent", "Error creating images directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
return
}
}

metaDir := fmt.Sprintf("%s/meta", roundDir)
if _, err := os.Stat(metaDir); os.IsNotExist(err) {
err = os.MkdirAll(metaDir, os.ModePerm)
if err != nil {
PrintIndexerError("processNFTMintedEvent", "Error creating meta directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
return
}
}


// Save image to disk
filename := fmt.Sprintf("nfts/images/nft-%d.png", tokenId)
filename := fmt.Sprintf("%s/nft-%d.png", imagesDir, tokenId)
file, err := os.Create(filename)
if err != nil {
PrintIndexerError("processNFTMintedEvent", "Error creating file", tokenIdLowHex, tokenIdHighHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
Expand Down Expand Up @@ -242,7 +254,7 @@ func processNFTMintedEvent(event IndexerEvent) {
return
}

metadataFilename := fmt.Sprintf("nfts/meta/nft-%d.json", tokenId)
metadataFilename := fmt.Sprintf("%s/nft-%d.json", metaDir, tokenId)
err = os.WriteFile(metadataFilename, metadataFile, 0644)
if err != nil {
PrintIndexerError("processNFTMintedEvent", "Error writing NFT metadata file", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter)
Expand Down
44 changes: 41 additions & 3 deletions backend/routes/nft.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package routes

import (
"context"
"fmt"
"strings"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -33,8 +36,43 @@ func InitNFTRoutes() {
}

func InitNFTStaticRoutes() {
http.Handle("/nft-images/", http.StripPrefix("/nft-images/", http.FileServer(http.Dir("./nfts/images"))))
http.Handle("/nft-meta/", http.StripPrefix("/nft-meta/", http.FileServer(http.Dir("./nfts/meta"))))
http.HandleFunc("/nft-images/", func(w http.ResponseWriter, r *http.Request) {
tokenID := strings.TrimPrefix(r.URL.Path, "/nft-images/nft-")
tokenID = strings.TrimSuffix(tokenID, ".png")

// Query postgres to get the day_index for this token
var dayIndex int
err := core.ArtPeaceBackend.Databases.Postgres.QueryRow(context.Background(),
"SELECT day_index FROM NFTs WHERE token_id = $1", tokenID).Scan(&dayIndex)
if err != nil {
http.NotFound(w, r)
return
}

roundNum := dayIndex/7 + 1
actualPath := fmt.Sprintf("./nfts/round-%d/images/nft-%s.png", roundNum, tokenID)

http.ServeFile(w, r, actualPath)
})

// Serve round-specific NFT metadata
http.HandleFunc("/nft-meta/", func(w http.ResponseWriter, r *http.Request) {
tokenID := strings.TrimPrefix(r.URL.Path, "/nft-meta/nft-")
tokenID = strings.TrimSuffix(tokenID, ".json")

var dayIndex int
err := core.ArtPeaceBackend.Databases.Postgres.QueryRow(context.Background(),
"SELECT day_index FROM NFTs WHERE token_id = $1", tokenID).Scan(&dayIndex)
if err != nil {
http.NotFound(w, r)
return
}

roundNum := dayIndex/7 + 1
actualPath := fmt.Sprintf("./nfts/round-%d/meta/nft-%s.json", roundNum, tokenID)

http.ServeFile(w, r, actualPath)
})
}

func getCanvasNFTAddress(w http.ResponseWriter, r *http.Request) {
Expand All @@ -50,7 +88,7 @@ func setCanvasNFTAddress(w http.ResponseWriter, r *http.Request) {

data, err := io.ReadAll(r.Body)
if err != nil {
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Failed to read reques t body")
routeutils.WriteErrorJson(w, http.StatusBadRequest, "Failed to read request body")
return
}
os.Setenv("CANVAS_NFT_CONTRACT_ADDRESS", string(data))
Expand Down

0 comments on commit 4c79c0e

Please sign in to comment.