-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created download functionality. #4
Open
mariacarmina
wants to merge
17
commits into
main
Choose a base branch
from
download-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+386
−31
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
df9b1bd
Created download functionality.
mariacarmina dc9ac21
fix import.
mariacarmina 9732dc2
Merge branch 'main' into download-feature
mariacarmina 94999d3
Add download to extension.
mariacarmina 4a71458
Configure extension.
mariacarmina abce3ef
Fix review.
mariacarmina 47ea8d4
Use private key once in configuration.
mariacarmina c709e46
Switch params order.
mariacarmina 4c992ba
Export command.
mariacarmina 3b7f947
Fix conflicts, merge scripts.
mariacarmina 77eff23
Clean ups.
mariacarmina 6ac9c85
Fix conflicts.
mariacarmina 1d08237
Fix extension.
mariacarmina 77ed322
Update library. Fix HTML.
mariacarmina e50a2fe
rollback.
mariacarmina 559a044
Used right config.
mariacarmina a5f6e3f
Fix.
mariacarmina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
Aquarius, | ||
Datatoken, | ||
ProviderInstance, | ||
ConfigHelper, | ||
orderAsset | ||
} from '@oceanprotocol/lib' | ||
import { Signer } from 'ethers' | ||
import { promises as fs } from 'fs' | ||
import * as path from 'path' | ||
|
||
export async function downloadFile( | ||
url: string, | ||
downloadPath: string, | ||
index?: number | ||
): Promise<any> { | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
throw new Error('Response error.') | ||
} | ||
|
||
const defaultName = !isNaN(index) && index > -1 ? `file_${index}.out` : 'file.out' | ||
let filename: string | ||
|
||
try { | ||
// try to get it from headers | ||
filename = response.headers | ||
.get('content-disposition') | ||
.match(/attachment;filename=(.+)/)[1] | ||
} catch { | ||
filename = defaultName | ||
} | ||
|
||
const filePath = path.join(downloadPath, filename) | ||
const data = await response.arrayBuffer() | ||
|
||
try { | ||
await fs.writeFile(filePath, Buffer.from(data)) | ||
} catch (err) { | ||
throw new Error('Error while saving the file:', err.message) | ||
} | ||
|
||
return filename | ||
} | ||
|
||
export async function download( | ||
did: string, | ||
owner: Signer, | ||
pathString: string = '.', | ||
aquariusInstance: Aquarius, | ||
macOsProviderUrl?: string, | ||
providerUrl?: string | ||
) { | ||
const dataDdo = await aquariusInstance.waitForAqua(did) | ||
if (!dataDdo) { | ||
console.error('Error fetching DDO ' + did + '. Does this asset exists?') | ||
return | ||
} | ||
let providerURI | ||
if (!providerUrl) { | ||
providerURI = | ||
macOsProviderUrl && dataDdo.chainId === 8996 | ||
? macOsProviderUrl | ||
: dataDdo.services[0].serviceEndpoint | ||
} else { | ||
providerURI = providerUrl | ||
} | ||
console.log('Downloading asset using provider: ', providerURI) | ||
const { chainId } = await owner.provider.getNetwork() | ||
console.log('Chain ID:', chainId) | ||
const config = new ConfigHelper().getConfig(chainId) | ||
console.log('Config:', config) | ||
const datatoken = new Datatoken(owner, chainId) | ||
|
||
const tx = await orderAsset(dataDdo, owner, config, datatoken, providerURI) | ||
|
||
if (!tx) { | ||
console.error('Error ordering access for ' + did + '. Do you have enough tokens?') | ||
return | ||
} | ||
|
||
const orderTx = await tx.wait() | ||
|
||
const urlDownloadUrl = await ProviderInstance.getDownloadUrl( | ||
dataDdo.id, | ||
dataDdo.services[0].id, | ||
0, | ||
orderTx.transactionHash, | ||
providerURI, | ||
owner | ||
) | ||
try { | ||
const path = pathString ? pathString : '.' | ||
const { filename } = await downloadFile(urlDownloadUrl, path) | ||
console.log('File downloaded successfully:', path + '/' + filename) | ||
} catch (e) { | ||
console.log(`Download url dataset failed: ${e}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by the way,
file path
is a bit of a confusing name as they don't have the file yet. It might be better to saydownload path
to indicate this is where the file is going to be saved.