-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
228 lines (198 loc) · 6.79 KB
/
background.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { createHelia } from 'helia';
import { unixfs } from '@helia/unixfs';
let helia;
let fs;
const ipfs_gateway = 'https://ipfs.cloud.fx.land';
let heliaRunning = false;
const initHelia = async () => {
try {
helia = await createHelia({
// Chrome extension specific configurations can be added here
});
fs = unixfs(helia);
console.log('Helia node is running');
return helia;
} catch (error) {
console.error('Failed to create Helia node:', error);
throw error;
}
};
async function ensureHeliaRunning() {
if (!heliaRunning) {
try {
await initHelia();
heliaRunning = true;
} catch (error) {
console.error('Failed to initialize Helia:', error);
throw error;
}
}
}
chrome.runtime.onInstalled.addListener(async function() {
try {
await initHelia();
chrome.contextMenus.create({
id: "saveToFula",
title: "Save to Fula",
contexts: ["image", "video"]
});
} catch (error) {
console.error('Failed to initialize Helia:', error);
}
});
chrome.contextMenus.onClicked.addListener(async function(info, tab) {
if (info.menuItemId === "saveToFula") {
try {
await ensureHeliaRunning();
const { apiKey } = await chrome.storage.local.get('apiKey');
if (!apiKey) {
throw new Error("API key not set. Please set up your API key first.");
}
await downloadAndPin(info.srcUrl, apiKey, tab);
} catch (error) {
console.error('Error:', error);
chrome.tabs.sendMessage(tab.id, { type: "error", message: error.message });
}
}
});
chrome.runtime.onStartup.addListener(() => {
heliaRunning = false;
});
chrome.runtime.onSuspend.addListener(() => {
heliaRunning = false;
});
async function downloadAndPin(url, apiKey, tab) {
const pinId = Date.now().toString();
try {
updatePinStatus(pinId, 'started', 'Saving the file...');
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log('object was fetched');
updatePinStatus(pinId, 'saved', 'Saved object locally');
const blob = await response.blob();
const buffer = await blob.arrayBuffer();
if (!fs) {
throw new Error('Helia is not initialized');
}
updatePinStatus(pinId, 'uploading', 'Uploading to Helia...');
const result = await fs.addBytes(new Uint8Array(buffer));
const cid = result.toString();
console.log('cid created from object: '+cid);
// Create file name
const filename = getFilenameFromUrl(url);
const pageUrl = tab.url || 'No URL';
const currentDateTime = new Date().toISOString();
const name = `fula-${pageUrl}-${currentDateTime}-${filename}`;
console.log('name of file='+name);
// Upload file to server
updatePinStatus(pinId, 'uploading', 'Uploading to server...');
const formData = new FormData();
formData.append('file', blob);
const serverResponse = await fetch(ipfs_gateway + '/upload', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`
},
body: formData
});
if (!serverResponse.ok) {
throw new Error(`Server upload failed: ${serverResponse.status}`);
}
const { cid: cid_server } = await serverResponse.json();
console.log('CID received from server:', cid_server);
if (cid !== cid_server) {
console.error('Received pin is different than expected');
updatePinStatus(pinId, 'error', 'Received pin is different than expected');
sendMessageToActiveTab({ type: "error", message: 'Received pin is different than expected' }, tab.id);
} else {
updatePinStatus(pinId, 'pinning', 'Pinning to Fula...');
await pinToFula(cid, apiKey, name);
updatePinStatus(pinId, 'pinned', 'Pinned successfully');
console.log('File successfully pinned to Fula');
sendMessageToActiveTab({
type: "success",
message: `File uploaded and pinned. CID: ${cid}`,
cid: cid
}, tab.id);
}
} catch (error) {
console.error('Failed to download and pin file:', error);
updatePinStatus(pinId, 'error', `Error: ${error.message}`);
sendMessageToActiveTab({ type: "error", message: error.message }, tab.id);
}
}
function getFilenameFromUrl(url) {
const urlParts = url.split('/');
let filename = urlParts[urlParts.length - 1];
filename = filename.split('?')[0];
filename = decodeURIComponent(filename);
return filename || 'unknown';
}
function updatePinStatus(id, status, message) {
chrome.storage.local.get('pinningStatus', (data) => {
const pinningStatus = data.pinningStatus || {};
pinningStatus[id] = { status, message, timestamp: Date.now() };
chrome.storage.local.set({ pinningStatus });
chrome.runtime.sendMessage({ action: 'pinningStatus', id, status, message });
});
}
function sendMessageToActiveTab(message) {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
if (chrome.runtime.lastError) {
console.log('Error sending message:', chrome.runtime.lastError);
} else {
console.log('Message sent successfully');
}
});
}
});
}
async function pinToFula(cid, apiKey, name) {
console.log(`Attempting to pin CID: ${cid} to Fula`);
try {
const sanitizedName = name.replace(/[^a-zA-Z0-9-_\.]/g, '_').substring(0, 255);
const response = await fetch('https://api.cloud.fx.land/pins', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
cid: cid,
name: sanitizedName
})
});
console.log(`Received response from Fula API. Status: ${response.status}`);
if (!response.ok) {
const errorText = await response.text();
console.error(`Failed to pin file. Status: ${response.status}, Error: ${errorText}`);
throw new Error(`Failed to pin file: ${response.status} - ${errorText}`);
}
const responseData = await response.json();
console.log(`Successfully pinned file. Response:`, responseData);
return responseData;
} catch (error) {
console.error(`Error in pinToFula function:`, error);
throw error;
}
}
console.log('Background script loaded');
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Background script received message:', request);
if (request.action === "initHelia") {
initHelia()
.then(() => {
console.log('Helia initialized successfully');
sendResponse({success: true});
})
.catch(error => {
console.error('Failed to initialize Helia:', error);
sendResponse({success: false, error: error.message});
});
return true; // Indicates that the response is asynchronous
}
});