-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from d0rich/frontend-files-download
Complete files download
- Loading branch information
Showing
21 changed files
with
319 additions
and
406 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"url": "https://bigfiles.cloud", | ||
"name": "BigFiles", | ||
"iconUrl": "https://bigfiles.cloud/drive.svg" | ||
"iconUrl": "https://bigfiles.cloud/icon.svg" | ||
} |
6 changes: 0 additions & 6 deletions
6
ton-drive-frontend/src/entities/file/utils/createDownloadLink.ts
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
ton-drive-frontend/src/features/file/api/createFileContract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Sender, TonClient } from "ton"; | ||
import {Address, Cell} from "ton-core"; | ||
import tonDrive, {storageProviderAddress} from "../../../services/FilesService"; | ||
import { apiConfig } from "../../../shared/config/api"; | ||
|
||
export interface CreateFileContractOptions { | ||
file: File; | ||
bagId: string; | ||
tonClient: { | ||
client: TonClient | undefined; | ||
}; | ||
sender: Sender; | ||
wallet: string; | ||
} | ||
|
||
// TODO: implement without calling backend | ||
export async function createFileContract({file, bagId, tonClient, sender, wallet}: CreateFileContractOptions) { | ||
if (!tonClient || !sender || !wallet) { | ||
throw new Error("Wallet not connected") | ||
} | ||
const tonDriveService = tonDrive(tonClient.client!!, sender) | ||
const contractParams = new URLSearchParams() | ||
contractParams.append('bagId', bagId) | ||
contractParams.append('providerAddress', storageProviderAddress.toRawString()) | ||
// TODO: use POST method body instead of query params | ||
const contractResponse = await fetch(new URL('contracts?' + contractParams.toString(), apiConfig.baseUrl).toString(), { | ||
method: 'POST' | ||
}) | ||
const contractFile = await contractResponse.arrayBuffer() | ||
|
||
const base64 = btoa(new Uint8Array(contractFile) | ||
.reduce((data, byte) => data + String.fromCharCode(byte), '')) | ||
|
||
const msgBody = Cell.fromBase64(base64) | ||
const slice = msgBody.beginParse() | ||
const opQueryId = slice.loadUint(32 + 64) | ||
const torrentHash = slice.loadRef().hash().toString('hex') | ||
console.log(`HEX: ${torrentHash}`, BigInt(`0x${torrentHash}`)) | ||
const userCollection = tonDriveService.userCollection(Address.parse(wallet)) | ||
return userCollection.createContract(msgBody, { | ||
name: file.name, | ||
size: file.size | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { apiConfig } from "../../../shared/config/api" | ||
|
||
export function getDownloadLink(bagId: string, fileName: string): string { | ||
return new URL(`download/${bagId}/${fileName}`, apiConfig.baseUrl).toString() | ||
} |
16 changes: 16 additions & 0 deletions
16
ton-drive-frontend/src/features/file/api/prepareDownload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { apiConfig } from "../../../shared/config/api"; | ||
|
||
export type PrepareDownloadResponse = { | ||
ready: boolean | ||
exists: boolean | ||
fileName: string | null | ||
_output?: string | object | ||
} | ||
|
||
export async function prepareDownload(bagId: string): Promise<PrepareDownloadResponse> { | ||
const resRaw = await fetch(new URL(`prepareDownload?bagId=${bagId}`, apiConfig.baseUrl).toString(), { | ||
method: 'POST' | ||
}) | ||
const res: PrepareDownloadResponse = await resRaw.json() | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,14 @@ | ||
import { Sender, TonClient } from "ton"; | ||
import {Address, Cell} from "ton-core"; | ||
import tonDrive, {storageProviderAddress} from "../../../services/FilesService"; | ||
|
||
export interface FileUploadOptions { | ||
file: File; | ||
tonClient: { | ||
client: TonClient | undefined; | ||
}; | ||
sender: Sender; | ||
wallet: string; | ||
} | ||
|
||
export async function uploadFile({ file, tonClient, sender, wallet }: FileUploadOptions) { | ||
const host = 'https://api.bigfiles.cloud' | ||
import { apiConfig } from "../../../shared/config/api"; | ||
|
||
export async function uploadFile(file: File) { | ||
const formData = new FormData() | ||
formData.append('file', file) | ||
|
||
if (!tonClient || !sender || !wallet) { | ||
console.log("Wallet not connected") | ||
return; | ||
} | ||
const tonDriveService = tonDrive(tonClient.client!!, sender) | ||
const response = await fetch(`${host}/upload`, { | ||
const response = await fetch(new URL('upload', apiConfig.baseUrl), { | ||
method: 'POST', | ||
body: formData | ||
}) | ||
|
||
const {bagId} = await response.json() as {bagId: string} | ||
const contractParams = new URLSearchParams() | ||
contractParams.append('bagId', bagId) | ||
contractParams.append('providerAddress', storageProviderAddress.toRawString()) | ||
// TODO: use POST method body instead of query params | ||
const contractResponse = await fetch(new URL('/contracts?' + contractParams.toString(), host).toString(), { | ||
method: 'POST' | ||
}) | ||
const contractFile = await contractResponse.arrayBuffer() | ||
|
||
const base64 = btoa(new Uint8Array(contractFile) | ||
.reduce((data, byte) => data + String.fromCharCode(byte), '')) | ||
|
||
const msgBody = Cell.fromBase64(base64) | ||
const slice = msgBody.beginParse() | ||
const opQueryId = slice.loadUint(32 + 64) | ||
const torrentHash = slice.loadRef().hash().toString('hex') | ||
console.log(`HEX: ${torrentHash}`, BigInt(`0x${torrentHash}`)) | ||
const userCollection = tonDriveService.userCollection(Address.parse(wallet)) | ||
return userCollection.createContract(msgBody, { | ||
name: file.name, | ||
size: file.size | ||
}) | ||
return bagId | ||
} |
Oops, something went wrong.