-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfs.cs
172 lines (128 loc) · 4.87 KB
/
ipfs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ipfs.Http;
using Ipfs;
using System.Security.Cryptography.X509Certificates;
namespace ExtBloq
{
public partial class ipfs : Form
{
private string filepath;
private IpfsClient IpfsClient;
public ipfs()
{
InitializeComponent();
IpfsClient = new IpfsClient();
}
public ipfs(string filepath):this()
{
this.filepath = filepath;
}
private void ipfs_Load(object sender, EventArgs e)
{
textBoxPath.Text = this.filepath;
labelFilePath.Text = this.filepath;
}
private async Task UploadFileToIpfs(string filePath)
{
// Create an instance of IpfsClient
var ipfs = new IpfsClient();
// Read the file into a byte array
byte[] fileBytes = File.ReadAllBytes(filePath);
// Upload the file to IPFS
var file = await ipfs.FileSystem.AddAsync(new MemoryStream(fileBytes));
// The 'file' variable now contains information about the uploaded file,
// including its hash, which can be used to retrieve it from IPFS.
Console.WriteLine($"Uploaded file. IPFS hash: {file.Id}");
textboxOutput.Text = file.Id;
// Since IpfsClient doesn't implement IDisposable, you don't need to dispose it.
}
private void SelectFile()
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "All Files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filepath = openFileDialog.FileName;
textBoxPath.Text = openFileDialog.FileName;
if (IsImageFile(filepath))
{
pictureBox1.Image = Image.FromFile(filepath);
}
else
{
pictureBox1.Image = pictureBoxFileSelect.Image;
MessageBox.Show("An image file was not selected, so akipping image preview.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void buttonSelect_Click(object sender, EventArgs e)
{
SelectFile();
}
private async void buttonUpload_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(filepath))
{
MessageBox.Show("Please select a file to upload.");
return;
}
try
{
await UploadFileToIpfs(filepath);
MessageBox.Show("File uploaded successfully to IPFS.");
}
catch (Exception ex)
{
MessageBox.Show($"Error uploading file to IPFS: {ex.Message}");
}
}
private async void RetrieveFileFromIpfs(string fileId)
{
try
{
// Retrieve the file from IPFS
var file = await IpfsClient.FileSystem.GetAsync(fileId);
// Save the file to a location of your choice
// For example, you can save it to the user's temporary folder
string recvFilePath = Path.Combine(Path.GetTempPath(), fileId);
using (var fileStream = File.Create(recvFilePath))
{
await file.CopyToAsync(fileStream);
}
MessageBox.Show($"File retrieved successfully. Path: {recvFilePath}");
}
catch (Exception ex)
{
MessageBox.Show($"Error retrieving file from IPFS: {ex.Message}");
}
}
private void buttonGet_Click(object sender, EventArgs e)
{
RetrieveFileFromIpfs(textboxReturn.Text);
}
private void textBoxPath_TextChanged(object sender, EventArgs e)
{
labelFilePath.Text = textBoxPath.Text;
}
private bool IsImageFile(string filePath)
{
string extension = Path.GetExtension(filePath);
return extension.Equals(".bmp", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".gif", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".png", StringComparison.OrdinalIgnoreCase);
}
}
}