|
|
|
@ -88,16 +88,14 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
|
|
|
|
|
if true {
|
|
|
|
|
|
|
|
|
|
// New invoice for 10$
|
|
|
|
|
invoice, err := ln.UploadInvoice(100, ln.CurSat, uploadId)
|
|
|
|
|
invoice, err := ln.NewInvoiceForUpload(100, ln.CurSat, uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
invoice.UploadId = uploadId
|
|
|
|
|
|
|
|
|
|
// Update Upload Invoice
|
|
|
|
|
err = SetUploadInvoice(invoice.UploadId, invoice)
|
|
|
|
|
err = SetUploadInvoice(uploadId, invoice)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
@ -144,6 +142,56 @@ func (ctrl UploadCtrl) CheckStatus(c *gin.Context) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
|
utils.JSONErr(c, http.StatusNotFound, "upload id not found")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get upload status
|
|
|
|
|
uploadStatus, err := GetUploadStatus(uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if uploadStatus.IsNew() {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"status": uploadStatus,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get invoice id for upload
|
|
|
|
|
invoiceId, err := GetUploadInvoiceId(uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get invoice
|
|
|
|
|
invoice, err := ln.CheckInvoice(invoiceId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(InvoiceHttpCode(invoice), gin.H{
|
|
|
|
|
"upload_id": uploadId,
|
|
|
|
|
"invoice": invoice,
|
|
|
|
|
"status": uploadStatus,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctrl UploadCtrl) PollStatus(c *gin.Context) {
|
|
|
|
|
uploadId := c.Param("id")
|
|
|
|
|
|
|
|
|
|
exists, err := IdExists(uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
|
utils.JSONErr(c, http.StatusNotFound, "id not found")
|
|
|
|
|
return
|
|
|
|
@ -162,29 +210,30 @@ func (ctrl UploadCtrl) CheckStatus(c *gin.Context) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if uploadStatus.WaitPay() {
|
|
|
|
|
// we are not in UpNew state so we should have an invoice
|
|
|
|
|
invoice, err := GetUploadInvoice(uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we should be in waiting payment meaning we have an invoice
|
|
|
|
|
invoice, err := GetUploadInvoice(uploadId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if uploadStatus.WaitPay() {
|
|
|
|
|
|
|
|
|
|
invoiceChan := make(chan *ln.Invoice)
|
|
|
|
|
errorChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
log.Println("starting go routine")
|
|
|
|
|
// If waiting payment, wait until invoice is paid
|
|
|
|
|
go ln.PollPaidInvoice(invoice, invoiceChan, errorChan)
|
|
|
|
|
go ln.PollPaidInvoice(invoice.Id, invoiceChan, errorChan)
|
|
|
|
|
|
|
|
|
|
// Block until payment done or error
|
|
|
|
|
log.Println("blocking")
|
|
|
|
|
select {
|
|
|
|
|
case invoice := <-invoiceChan:
|
|
|
|
|
|
|
|
|
|
// if expired return with expired response
|
|
|
|
|
if invoice.Expired {
|
|
|
|
|
err := SetUploadStatus(invoice.UploadId, UpPayExpired)
|
|
|
|
|
err := SetUploadStatus(uploadId, UpPayExpired)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Redis error: %s", err)
|
|
|
|
|
}
|
|
|
|
@ -202,13 +251,13 @@ func (ctrl UploadCtrl) CheckStatus(c *gin.Context) {
|
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
|
|
uploadStatus |= UpPaid
|
|
|
|
|
err := SetUploadStatus(invoice.UploadId, uploadStatus)
|
|
|
|
|
err := SetUploadStatus(uploadId, uploadStatus)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Redis error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update Upload Invoice
|
|
|
|
|
err = SetUploadInvoice(invoice.UploadId, invoice)
|
|
|
|
|
err = SetUploadInvoice(uploadId, invoice)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
@ -224,12 +273,12 @@ func (ctrl UploadCtrl) CheckStatus(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
case err := <-errorChan:
|
|
|
|
|
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"status": uploadStatus,
|
|
|
|
|
"status": uploadStatus,
|
|
|
|
|
"invoice": invoice,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|