Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Part 1 of Download File from Service Provider Progress
- TS code dynamically adds Buttons when message is recieved. Service Provider Download TODO
  • Loading branch information
joeiacono2021 committed Mar 8, 2023
commit fd0f5fe5405379bb73bfffba839637b55577cee4
10 changes: 6 additions & 4 deletions ipfs-service-provider-tutorial/service-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import WebSocket, { MessageEvent } from "ws";
import { IPFS, create } from 'ipfs-core'
import type { CID } from 'multiformats/cid'
import { create } from 'ipfs-core'
import fetch from 'node-fetch';

var ourAddress: string;
Expand Down Expand Up @@ -57,7 +56,6 @@ function handleResponse(responseMessageEvent : MessageEvent) {
console.log('\x1b[93mUploading file to IPFS... \x1b[0m')

uploadToIPFS(messageContent,response.senderTag)

}
} catch (_) {
console.log('something went wrong in handleResponse')
Expand Down Expand Up @@ -142,10 +140,14 @@ function readFileSize(bytes : number, si=false, dp=1) {
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);


return bytes.toFixed(dp) + ' ' + units[u];
}

async function getAndSendBackFile(cid : string){
let file = await ipfsNode.get(cid);
console.log(file);
}

// Function that connects our application to the mixnet Websocket. We want to call this first in our main function.
function connectWebsocket(url : string) {
return new Promise(function (resolve, reject) {
Expand Down
33 changes: 32 additions & 1 deletion ipfs-service-provider-tutorial/user-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function sendMessageToMixnet(payload) {
/*
Display responses into our activity log.
*/
function displayJsonSend(message) {
function displayJsonSend(message){

let sendDiv = document.createElement("div")
let messageLog = document.createElement("p")
Expand Down Expand Up @@ -187,6 +187,8 @@ function displayJsonResponse(message) {
let line1Contents;
let line2Contents;

var downloadFileButton = document.createElement("button");

if (message.type == 'selfAddress'){
//Display our self address.
ourAddress = message.address;
Expand All @@ -209,14 +211,43 @@ function displayJsonResponse(message) {

line1Contents = document.createTextNode("⬇ " + dataLog.time + " | " + dataLog.name);
line2Contents = document.createTextNode('Link: ' + dataLog.url);

downloadFileButton.innerHTML = 'Download File';
downloadFileButton.onclick = function()
{
alert("hello, world");
}
}

messageLine1.appendChild(line1Contents);
messageLine2.appendChild(line2Contents);

receivedDiv.appendChild(messageLine1);
receivedDiv.appendChild(messageLine2);

if (message.type == 'received'){
receivedDiv.appendChild(downloadFileButton);
}

document.getElementById("output").appendChild(receivedDiv);
}

async function sendDownloadRequest(cid : string){
var messageContentToSend = {
fileCid : cid
};

/*We have to send a string to the mixnet for it to be a valid message , so we use JSON.stringify to make our object into a string.*/
const message = {
type: "sendAnonymous",
message: JSON.stringify(messageContentToSend),
recipient: targetAddress,
replySurbs: 5
}

displayClientMessage('Download request for file with hash ' + cid + ' sent.')
//Send our message object via out via our websocket connection.
websocketConnection.send(JSON.stringify(message));
}

main();