Implemented developer database downloads

This commit is contained in:
2019-09-07 19:56:13 +09:00
parent 59b9be5992
commit 4e6aa5eef6
33 changed files with 155 additions and 46 deletions

View File

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

View File

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

View File

@ -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)

View File

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