Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lighthouse <img src="https://img.shields.io/badge/v0.4.1-green"/>
# Lighthouse <img src="https://img.shields.io/badge/v0.4.2-green"/>

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.

Expand Down Expand Up @@ -35,6 +35,7 @@ lighthouse-web3 deal-status <cid> # Get filecoin deal status of a

# File management
lighthouse-web3 get-uploads # Get details of files uploaded
lighthouse-web3 delete-file <fileID> # Delete a file

# Sharing and access control
lighthouse-web3 share-file <cid> <address> # Share access to another user
Expand Down Expand Up @@ -80,3 +81,4 @@ This project is tested with [BrowserStack](https://www.browserstack.com/).
## License

[MIT](https://choosealicense.com/licenses/mit/)
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/Commands/delete-file.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 8 additions & 1 deletion src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -158,6 +159,12 @@ widgets
.description('Get details of file uploaded')
.action(getUploads)

widgets
.command('delete-file')
.description('Delete a file')
.argument('<fileID>', 'File ID')
.action(deleteFile)

widgets.addHelpText(
'after',
'\r\nExample:' +
Expand Down
40 changes: 40 additions & 0 deletions src/Lighthouse/deleteFile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { lighthouseConfig } from '../../lighthouse.config'

export type deleteFileResponseType = {
data: {
message: string
}
}

export default async (
authToken: string,
fileId: string
): Promise<deleteFileResponseType> => {
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)
}
}
3 changes: 3 additions & 0 deletions src/Lighthouse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -64,6 +65,7 @@ export {
getAllKeys,
removeKey,
posdi,
deleteFile,
}

export default {
Expand Down Expand Up @@ -94,4 +96,5 @@ export default {
getAllKeys,
removeKey,
posdi,
deleteFile,
}
36 changes: 36 additions & 0 deletions src/Lighthouse/tests/deleteFile.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})