diff --git a/api/download.go b/api/download.go index 5af3e81..93fa854 100644 --- a/api/download.go +++ b/api/download.go @@ -14,7 +14,8 @@ import ( "github.com/gin-gonic/gin" ) -func DownloadHandler(c *gin.Context) { +func download(c *gin.Context) { + var ok bool dlId := c.Param("dlId") @@ -32,13 +33,35 @@ func DownloadHandler(c *gin.Context) { return } - dlSess, exists := session.Values["session-id"] + dlSess := session.Values["session-id"] + dlIdVal := session.Values["dlId"] + + var sessDlId string + + // If no dlId was stored before than this is a new download + if dlIdVal == nil { + sessDlId = dlId + } else { + + if sessDlId, ok = dlIdVal.(string); !ok { + log.Printf("cannot cast sessDlId value") + utils.JSONErr(c, http.StatusExpectationFailed, + "start a new download session session") + return + } + + } // Test if we are alread in a download session - if !exists { + // If the stored dlId is different than the + // current dl id it means we're downloading a different + // file + if dlSess == nil || (sessDlId != dlId) { // This is a new download session log.Println("new download session") + session.Values["dlId"] = dlId + // Create a unique session id for this download session sessId, err := storage.GetShortId() if err != nil { @@ -73,12 +96,16 @@ func DownloadHandler(c *gin.Context) { Memo: fmt.Sprintf("bit4sat download: %s", sessId), } + log.Printf("invoice opts %#v", invoiceOpts) + invoice, err := ln.NewInvoice(invoiceOpts) if err != nil { utils.JSONErrPriv(c, http.StatusInternalServerError, err) return } + // Store invoice + session.Values["session-id"] = sessId session.Values["invoice"] = invoice @@ -104,8 +131,6 @@ func DownloadHandler(c *gin.Context) { } else { log.Printf("continue download session id: %s", dlSess) - var ok bool - invVal := session.Values["invoice"] var invoice = &ln.Invoice{} if invoice, ok = invVal.(*ln.Invoice); !ok { @@ -124,6 +149,9 @@ func DownloadHandler(c *gin.Context) { } //TODO: Check if invoice paid ?? + // + + //If paid return the files else return the invoice to pay filesVal := session.Values["files"] var filesMeta = &[]storage.FileUpload{} diff --git a/api/handlers.go b/api/handlers.go index bdb9373..61a842b 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -7,6 +7,7 @@ import ( "git.sp4ke.com/sp4ke/bit4sat/db" "git.sp4ke.com/sp4ke/bit4sat/ln" + "git.sp4ke.com/sp4ke/bit4sat/lndrpc" "git.sp4ke.com/sp4ke/bit4sat/storage" "git.sp4ke.com/sp4ke/bit4sat/utils" "github.com/gin-gonic/gin" @@ -68,6 +69,21 @@ func upSessionHandler(c *gin.Context) { return } +func pollInvoice(c *gin.Context) { + rhash := c.Params("rhash") + + // First check if already paid + lnInv, err := lndrpc.LookupInvoiceRhashStr(invoiceId) + if err != nil { + log.Println(err) + utils.JSONErr(c, http.StatusNotFound, + "invoice not found !") + return + } + + // if not poll the status +} + // Was used by ln-charge func invoiceCbHandler(c *gin.Context) { invoice := ln.Invoice{} diff --git a/api/routes.go b/api/routes.go index 4a6177d..aa914b4 100644 --- a/api/routes.go +++ b/api/routes.go @@ -31,12 +31,16 @@ func (api *API) Run() { uploadRoute.GET("/poll/:id", upCtrl.PollStatus) } + // Invoice poller + // invoice id is the RHash field + v1.GET("/pollinvoice/:rhash", pollInvoice) + // Download route downRoute := v1.Group("/d") { // Download - downRoute.GET(":dlId", DownloadHandler) + downRoute.GET(":dlId", download) } // test rout diff --git a/ln/invoice.go b/ln/invoice.go index 6412e57..b94ac93 100644 --- a/ln/invoice.go +++ b/ln/invoice.go @@ -117,8 +117,8 @@ type Invoice struct { CreatedAt timestamp `json:"created_at"` ExpiresAt timestamp `json:"expires_at"` Expired bool `json:"-"` - QuotedCurrency string `json:"quoted_currency"` - QuotedAmount float64 `json:"quoted_amount"` + QuotedCurrency string `json:"-"` + QuotedAmount float64 `json:"-"` } func (i Invoice) MarshalBinary() ([]byte, error) { @@ -132,6 +132,8 @@ func (i *Invoice) UnmarshalBinary(b []byte) error { // Create new Invoice from lnrpc.Invoice func InvoiceFromLndIn(lndInvoice *lnrpc.Invoice) *Invoice { + log.Printf("new invoice form lnd with value %d", lndInvoice.Value) + log.Printf("setting msat to %d", float64(lndInvoice.Value*1000)) invoice := Invoice{ AddIndex: lndInvoice.AddIndex, Description: lndInvoice.GetMemo(), diff --git a/storage/upload_model.go b/storage/upload_model.go index 878d27e..adba932 100644 --- a/storage/upload_model.go +++ b/storage/upload_model.go @@ -585,9 +585,9 @@ func (u *FileUpload) TxWrite(tx *sqlx.Tx) error { func (u *Upload) TxWrite(tx *sqlx.Tx) error { query := `INSERT INTO uploads - (upload_id, download_id, ask_fee, ask_currency, settled) + (upload_id, download_id, ask_fee, ask_currency, ask_amount, settled) VALUES - (:upload_id, :download_id, :ask_fee, :ask_currency, :settled)` + (:upload_id, :download_id, :ask_fee, :ask_currency, :ask_amount, :settled)` _, err := tx.NamedExec(query, u) diff --git a/web/src/UploadView.vue b/web/src/UploadView.vue index 76c169d..854af6c 100644 --- a/web/src/UploadView.vue +++ b/web/src/UploadView.vue @@ -50,36 +50,43 @@ export default { // First check the status to get the invoice Api.checkUploadStatus(this.uploadId) - .then((data)=>{ + .then((res)=>{ + + return res.json() + .then((data)=>{ + this.$store.commit('setInvoice', data.invoice) - this.$store.commit('setInvoice', data.invoice) + // Set upload metadata + this.$store.commit('setUpStatus', data.status) - // Set upload metadata - this.$store.commit('setUpStatus', data.status) + return data + }) + }) + .catch((err)=>{ + console.error(err) + }) + .then((data)=>{ // if payment required, poll for invoice paid if (data.status.pay_status === 'waiting') { - Api.pollUploadStatus(this.uploadId) + Api.pollUploadStatus(self.uploadId) + .then((res)=>{ return res.json() }) .then((data)=>{ + + console.log(data) - this.$store.commit('setInvoice', data.invoice) - this.$store.commit('setUpStatus', data.status) + self.$store.commit('setInvoice', data.invoice) + self.$store.commit('setUpStatus', data.status) // if paid we get the admin/dl link if (data.status.pay_status == 'paid' ){ - this.accepted = true; + self.accepted = true; - ({ admin_token: this.adminToken, - download_id: this.downloadId } = data); + ({ admin_token: self.adminToken, + download_id: self.downloadId } = data); } - }) } - - - }) - .catch((err)=>{ - console.error(err) }) diff --git a/web/src/api.js b/web/src/api.js index be6d351..b7d5b77 100644 --- a/web/src/api.js +++ b/web/src/api.js @@ -59,11 +59,7 @@ export async function pollUploadStatus(uploadId){ credentials: 'same-origin' }) - let res = await fetch(req) - .catch((e) => {console.error(e)}) - - - return res.json().catch((e)=>{console.error(e)}) + return fetch(req).catch((e)=>{console.error(e)}) }