|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/db"
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/ln"
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/storage"
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/utils"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/mediocregopher/radix/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sessionHandler(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
|
|
|
|
lastUp := session.Get(storage.SessLastUploadName)
|
|
|
|
session.Clear()
|
|
|
|
session.Save()
|
|
|
|
|
|
|
|
if lastUp != nil {
|
|
|
|
|
|
|
|
// Get upload status
|
|
|
|
key := fmt.Sprintf("upload_status_%s", lastUp.(string))
|
|
|
|
|
|
|
|
var upStatus storage.UpStatus
|
|
|
|
err := db.DB.Redis.Do(radix.FlatCmd(&upStatus, "GET", key))
|
|
|
|
if err != nil {
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError,
|
|
|
|
fmt.Errorf("error finding upload status %s", lastUp))
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice, err := storage.GetUploadInvoice(lastUp.(string))
|
|
|
|
if err != nil {
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if invoice is not empty
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"uploadId": lastUp,
|
|
|
|
"status": upStatus,
|
|
|
|
"invoice": invoice,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"uploadId": 0,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func invoiceCbHandler(c *gin.Context) {
|
|
|
|
invoice := ln.Invoice{}
|
|
|
|
|
|
|
|
//dec := json.Decoder(c.Request.Body)
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&invoice); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
utils.JSONErr(c, http.StatusNotAcceptable,
|
|
|
|
"callback: could not parse request from charge api")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("received invoice paid from ln charge\n%s", invoice)
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
|
|
|
|
// get upload id related to invoice
|
|
|
|
var uploadId string
|
|
|
|
invoiceUploadKey := fmt.Sprintf("invoice_%s_upload", invoice.RHash)
|
|
|
|
|
|
|
|
err := db.DB.Redis.Do(radix.FlatCmd(&uploadId, "GET", invoiceUploadKey))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if uploadId == "" {
|
|
|
|
log.Printf("cannot find uploadId for invoice %s", invoice)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoice paid !!!!
|
|
|
|
|
|
|
|
// Set upload status to paid
|
|
|
|
err = storage.SetUploadStatus(uploadId, storage.UpPaid)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Update Upload Invoice
|
|
|
|
err = storage.SetUploadInvoice(uploadId, &invoice)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|