Skip to content

Commit

Permalink
[PSL-1183] upload file v2 (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateeullahmalik authored Jun 21, 2024
1 parent 3a9f160 commit a25cb63
Show file tree
Hide file tree
Showing 25 changed files with 51,161 additions and 66,581 deletions.
4 changes: 2 additions & 2 deletions common/storage/ticketstore/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type FilesQueries interface {
func (s *TicketStore) UpsertFile(file types.File) error {
const upsertQuery = `
INSERT INTO files (
file_id, upload_timestamp, path, index, base_file_id, task_id,
file_id, upload_timestamp, path, file_index, base_file_id, task_id,
reg_txid, activation_txid, req_burn_txn_amount, burn_txn_id,
req_amount, is_concluded, cascade_metadata_ticket_id, uuid_key,
hash_of_original_big_file, name_of_original_big_file_with_ext,
Expand All @@ -25,7 +25,7 @@ func (s *TicketStore) UpsertFile(file types.File) error {
DO UPDATE SET
upload_timestamp = excluded.upload_timestamp,
path = excluded.path,
file_index = excluded.index,
file_index = excluded.file_index,
base_file_id = excluded.base_file_id,
task_id = excluded.task_id,
reg_txid = excluded.reg_txid,
Expand Down
16 changes: 16 additions & 0 deletions common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"container/heap"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/hex"
Expand Down Expand Up @@ -179,6 +180,21 @@ func GetHashFromString(inputString string) string {
return hex.EncodeToString(h.Sum(nil))
}

func ComputeSHA256HashOfFile(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return nil, err
}

return hasher.Sum(nil), nil
}

// XORBytes returns the XOR of two same-length byte slices.
func XORBytes(a, b []byte) ([]byte, error) {
if len(a) != len(b) {
Expand Down
81 changes: 81 additions & 0 deletions walletnode/api/design/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ var _ = Service("cascade", func() {
})
})

Method("uploadAssetV2", func() {
Description("Upload the asset file - This endpoint is for the new version of the upload endpoint that supports larger files as well.")
Meta("swagger:summary", "Uploads Cascade File")

Payload(func() {
Extend(AssetUploadPayloadV2)
})
Result(AssetUploadResultV2)

HTTP(func() {
POST("/v2/upload")
MultipartRequest()

// Define error HTTP statuses.
Response("BadRequest", StatusBadRequest)
Response("InternalServerError", StatusInternalServerError)
Response(StatusCreated)
})
})

Method("startProcessing", func() {
Description("Start processing the image")
Meta("swagger:summary", "Starts processing the image")
Expand Down Expand Up @@ -160,6 +180,42 @@ var AssetUploadPayload = Type("AssetUploadPayload", func() {
Meta("swagger:example", "false")
Description("For internal use")
})

Attribute("hash", String, func() {
Meta("swagger:example", "false")
Description("For internal use")
})

Attribute("size", Int64, func() {
Meta("swagger:example", "false")
Description("For internal use")
})

Required("file")
})

// AssetUploadPayload represents a payload for uploading asset
var AssetUploadPayloadV2 = Type("AssetUploadPayloadV2", func() {
Description("Asset upload payload")
Attribute("file", Bytes, func() {
Meta("struct:field:name", "Bytes")
Description("File to upload")
})
Attribute("filename", String, func() {
Meta("swagger:example", "false")
Description("-For internal use-")
})

Attribute("hash", String, func() {
Meta("swagger:example", "false")
Description("For internal use")
})

Attribute("size", Int64, func() {
Meta("swagger:example", "false")
Description("For internal use")
})

Required("file")
})

Expand Down Expand Up @@ -196,6 +252,31 @@ var AssetUploadResult = ResultType("application/vnd.cascade.upload-file", func()
Required("file_id", "expires_in", "total_estimated_fee")
})

var AssetUploadResultV2 = ResultType("application/vnd.cascade.upload-file-v2", func() {
TypeName("AssetV2")
Attributes(func() {
Attribute("file_id", String, func() {
Description("Uploaded file ID")
MinLength(8)
MaxLength(8)
Example("VK7mpAqZ")
})

Attribute("total_estimated_fee", Float64, func() {
Description("Estimated fee")
Minimum(0.00001)
Default(1)
Example(100)
})

Attribute("required_preburn_transaction_amounts", ArrayOf(Float64), func() {
Description("The amounts that's required to be preburned - one per transaction")
})
})

Required("file_id", "total_estimated_fee")
})

// StartCascadeProcessingPayload - Payload for starting processing
var StartCascadeProcessingPayload = Type("StartCascadeProcessingPayload", func() {
Description("Start Processing Payload")
Expand Down
20 changes: 19 additions & 1 deletion walletnode/api/gen/cascade/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions walletnode/api/gen/cascade/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 80 additions & 1 deletion walletnode/api/gen/cascade/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a25cb63

Please sign in to comment.