diff --git a/README.md b/README.md
index e60ebbf..ab27c04 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# Lighthouse
+# Lighthouse
Lighthouse is a permanent decentralized file storage protocol that allows the ability to pay once and store forever. While traditionally, users need to repeatedly keep track and pay for their storage after every fixed amount of time, Lighthouse manages this for them and makes sure that user files are stored forever. The aim is to move users from a rent-based cost model where they are renting their own files on cloud storage to a permanent ownership model. It is built on top of IPFS, Filecoin, and Polygon. It uses the existing miner network and storage capacity of the filecoin network.
@@ -35,6 +35,7 @@ lighthouse-web3 deal-status # Get filecoin deal status of a
# File management
lighthouse-web3 get-uploads # Get details of files uploaded
+lighthouse-web3 delete-file # Delete a file
# Sharing and access control
lighthouse-web3 share-file # Share access to another user
@@ -80,3 +81,4 @@ This project is tested with [BrowserStack](https://www.browserstack.com/).
## License
[MIT](https://choosealicense.com/licenses/mit/)
+```
diff --git a/package-lock.json b/package-lock.json
index f51e238..96cbbb2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@lighthouse-web3/sdk",
- "version": "0.4.1",
+ "version": "0.4.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@lighthouse-web3/sdk",
- "version": "0.4.1",
+ "version": "0.4.2",
"license": "MIT",
"dependencies": {
"@lighthouse-web3/kavach": "^0.1.9",
diff --git a/package.json b/package.json
index 1a28d5a..f319fdb 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@lighthouse-web3/sdk",
- "version": "0.4.1",
+ "version": "0.4.2",
"description": "NPM package and CLI tool to interact with lighthouse protocol",
"main": "./dist/Lighthouse/index.js",
"types": "./dist/Lighthouse/index.d.ts",
diff --git a/src/Commands/delete-file.ts b/src/Commands/delete-file.ts
new file mode 100644
index 0000000..58026e0
--- /dev/null
+++ b/src/Commands/delete-file.ts
@@ -0,0 +1,25 @@
+import { yellow, red, green } from 'kleur'
+import { config } from './utils/getNetwork'
+import lighthouse from '../Lighthouse'
+
+export default async function (fileId: string) {
+ try {
+ if (!config.get('LIGHTHOUSE_GLOBAL_API_KEY')) {
+ throw new Error('Please create api-key first: use api-key command')
+ }
+
+ if (!fileId) {
+ throw new Error('Please provide a file ID to delete.')
+ }
+
+ const response = await lighthouse.deleteFile(
+ config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string,
+ fileId
+ )
+
+ console.log(green('Success: ') + yellow(response.data.message))
+ } catch (error: any) {
+ console.log(red(error.message))
+ process.exit(0)
+ }
+}
diff --git a/src/Commands/index.ts b/src/Commands/index.ts
index 34acd0a..b1cfb13 100644
--- a/src/Commands/index.ts
+++ b/src/Commands/index.ts
@@ -9,6 +9,7 @@ import apiKey from './api-key'
import balance from './balance'
import shareFile from './share-file'
import getUploads from './get-uploads'
+import deleteFile from './delete-file'
import dealStatus from './deal-status'
import decryptFile from './decrypt-file'
import createWallet from './create-wallet'
@@ -71,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) {
}
widgets.addHelpText('before', 'Welcome to lighthouse-web3')
-widgets.version('0.4.1')
+widgets.version('0.4.2')
widgets
.command('wallet')
@@ -158,6 +159,12 @@ widgets
.description('Get details of file uploaded')
.action(getUploads)
+widgets
+ .command('delete-file')
+ .description('Delete a file')
+ .argument('', 'File ID')
+ .action(deleteFile)
+
widgets.addHelpText(
'after',
'\r\nExample:' +
diff --git a/src/Lighthouse/deleteFile/index.ts b/src/Lighthouse/deleteFile/index.ts
new file mode 100644
index 0000000..bde4d23
--- /dev/null
+++ b/src/Lighthouse/deleteFile/index.ts
@@ -0,0 +1,40 @@
+import { lighthouseConfig } from '../../lighthouse.config'
+
+export type deleteFileResponseType = {
+ data: {
+ message: string
+ }
+}
+
+export default async (
+ authToken: string,
+ fileId: string
+): Promise => {
+ try {
+ const response = await fetch(
+ `${lighthouseConfig.lighthouseAPI}/api/user/delete_file?id=${fileId}`,
+ {
+ method: 'DELETE',
+ headers: {
+ Authorization: `Bearer ${authToken}`,
+ 'Content-Type': 'application/json',
+ },
+ }
+ )
+
+ if (!response.ok) {
+ throw new Error(`Request failed with status code ${response.status}`)
+ }
+
+ const result = (await response.json()) as any
+ /*
+ Example response:
+ {
+ "message": "File deleted successfully."
+ }
+ */
+ return { data: result }
+ } catch (error: any) {
+ throw new Error(error.message)
+ }
+}
diff --git a/src/Lighthouse/index.ts b/src/Lighthouse/index.ts
index 1ab2806..1edaa21 100644
--- a/src/Lighthouse/index.ts
+++ b/src/Lighthouse/index.ts
@@ -5,6 +5,7 @@ import dealStatus from './dealStatus'
import getUploads from './getUploads'
import getFileInfo from './getFileInfo'
import createWallet from './createWallet'
+import deleteFile from './deleteFile'
// Pay per deal
import fund from './payPerDeal/fund'
@@ -64,6 +65,7 @@ export {
getAllKeys,
removeKey,
posdi,
+ deleteFile,
}
export default {
@@ -94,4 +96,5 @@ export default {
getAllKeys,
removeKey,
posdi,
+ deleteFile,
}
diff --git a/src/Lighthouse/tests/deleteFile.test.ts b/src/Lighthouse/tests/deleteFile.test.ts
new file mode 100644
index 0000000..f72968e
--- /dev/null
+++ b/src/Lighthouse/tests/deleteFile.test.ts
@@ -0,0 +1,36 @@
+import lighthouse from '..'
+import 'dotenv/config'
+
+describe('deleteFile', () => {
+ const apiKey = process.env.TEST_API_KEY as string
+ const fileId = process.env.TEST_FILE_ID as string
+
+ it('should delete a file with correct API key and file ID', async () => {
+ const uploadsRes = await lighthouse.getUploads(apiKey)
+ const fileList = uploadsRes.data.fileList
+ expect(fileList.length).toBeGreaterThan(0)
+
+ const firstFileId = fileList[0].id
+ expect(typeof firstFileId).toBe('string')
+
+ const res = await lighthouse.deleteFile(apiKey, firstFileId)
+ expect(res.data.message).toBe('File deleted successfully.')
+ }, 60000)
+
+ it('should not delete a file with invalid API key', async () => {
+ try {
+ await lighthouse.deleteFile('invalid.APIKey', fileId)
+ } catch (error: any) {
+ expect(error.message).toBe('Request failed with status code 401')
+ }
+ }, 60000)
+
+ it('should not delete a file with invalid file ID', async () => {
+ try {
+ await lighthouse.deleteFile(apiKey, 'invalid-file-id')
+ } catch (error: any) {
+ // The error message may vary depending on API response
+ expect(error.message).toMatch(/Request failed with status code/i)
+ }
+ }, 60000)
+})