Implemented developer database downloads
This commit is contained in:
@ -1,10 +1,90 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/aerogo/api"
|
||||
"github.com/akyoto/stringutils/unsafe"
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
"github.com/mohae/deepcopy"
|
||||
)
|
||||
|
||||
// privateTypes are types that are not available for download.
|
||||
var privateTypes = []string{
|
||||
"EditLogEntry",
|
||||
"EmailToUser",
|
||||
"PayPalPayment",
|
||||
"Purchase",
|
||||
"Session",
|
||||
}
|
||||
|
||||
// Download downloads a snapshot of a database collection.
|
||||
func Download(ctx aero.Context) error {
|
||||
return nil
|
||||
typ := ctx.Get("type")
|
||||
|
||||
if !arn.DB.HasType(typ) {
|
||||
return ctx.Error(http.StatusNotFound, "Type doesn't exist")
|
||||
}
|
||||
|
||||
if arn.Contains(privateTypes, typ) {
|
||||
return ctx.Error(http.StatusUnauthorized, "Type is private and can not be downloaded")
|
||||
}
|
||||
|
||||
// Send headers necessary for file downloads
|
||||
ctx.Response().SetHeader("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.dat"`, typ))
|
||||
|
||||
// Stream data
|
||||
reader, writer := io.Pipe()
|
||||
encoder := json.NewEncoder(writer)
|
||||
|
||||
go func() {
|
||||
for object := range arn.DB.All(typ) {
|
||||
idObject, hasID := object.(arn.Identifiable)
|
||||
|
||||
if !hasID {
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter out private data
|
||||
filter, isFilter := object.(api.Filter)
|
||||
|
||||
if isFilter && filter.ShouldFilter(ctx) {
|
||||
object = deepcopy.Copy(object)
|
||||
filter = object.(api.Filter)
|
||||
filter.Filter()
|
||||
}
|
||||
|
||||
// Write ID
|
||||
_, err := writer.Write(unsafe.StringToBytes(idObject.GetID()))
|
||||
|
||||
if err != nil {
|
||||
_ = writer.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Write newline
|
||||
_, err = writer.Write([]byte("\n"))
|
||||
|
||||
if err != nil {
|
||||
_ = writer.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Write JSON (newline included)
|
||||
err = encoder.Encode(object)
|
||||
|
||||
if err != nil {
|
||||
_ = writer.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
writer.Close()
|
||||
}()
|
||||
|
||||
return ctx.Reader(reader)
|
||||
}
|
||||
|
@ -13,6 +13,10 @@ func Types(ctx aero.Context) error {
|
||||
types := make([]string, 0, len(typeMap))
|
||||
|
||||
for typeName := range typeMap {
|
||||
if arn.Contains(privateTypes, typeName) {
|
||||
continue
|
||||
}
|
||||
|
||||
types = append(types, typeName)
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ func Register(app *aero.Application) {
|
||||
|
||||
// Types
|
||||
app.Get("/api/types", database.Types)
|
||||
app.Get("/api/types/:type/all", database.Download)
|
||||
app.Get("/api/types/:type/download", database.Download)
|
||||
|
||||
// SoundTrack
|
||||
app.Post("/api/soundtrack/:id/download", soundtrack.Download)
|
||||
|
@ -63,19 +63,8 @@ func Success(ctx aero.Context) error {
|
||||
}
|
||||
|
||||
stringutils.PrettyPrint(sdkPayment)
|
||||
|
||||
transaction := sdkPayment.Transactions[0]
|
||||
|
||||
payment := &arn.PayPalPayment{
|
||||
ID: paymentID,
|
||||
PayerID: payerID,
|
||||
UserID: user.ID,
|
||||
Method: sdkPayment.Payer.PaymentMethod,
|
||||
Amount: transaction.Amount.Total,
|
||||
Currency: transaction.Amount.Currency,
|
||||
Created: arn.DateTimeUTC(),
|
||||
}
|
||||
|
||||
payment := arn.NewPayPalPayment(paymentID, payerID, user.ID, sdkPayment.Payer.PaymentMethod, transaction.Amount.Total, transaction.Amount.Currency)
|
||||
payment.Save()
|
||||
|
||||
// Increase user's balance
|
||||
|
Reference in New Issue
Block a user