|
|
|
@ -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{}
|
|
|
|
|