-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from ardriveapp/dev
Release 1.20.0
- Loading branch information
Showing
9 changed files
with
307 additions
and
12 deletions.
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
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
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,33 @@ | ||
import { expect } from 'chai'; | ||
import { cleanUpTempFolder, getTempFolder } from './temp_folder'; | ||
import { downloadFile } from './download_file'; | ||
import * as fs from 'fs'; | ||
|
||
describe('downloadFile function', () => { | ||
const validDownloadLink = 'https://arweave.net/pVoSqZgJUCiNw7oS6CtlVEd8gREQlpRbccrsMLkeIuQ'; | ||
const invalidDownloadLink = 'https://arweave.net/pVoSqZgJUCiNw7oS6CtlVEV8gREQlpRbccrsMLkeIuQ'; | ||
const destinationFileName = 'cat.jpg'; | ||
const tempFolderPath = getTempFolder(); | ||
it('downloads a file into the provided folder when given a valid link', async () => { | ||
const { pathToFile, contentType } = await downloadFile(validDownloadLink, tempFolderPath, destinationFileName); | ||
expect(fs.existsSync(pathToFile)).to.equal(true); | ||
expect(contentType).to.equal('image/jpeg'); | ||
}); | ||
|
||
it('download throws when given an invalid link', async () => { | ||
let error; | ||
try { | ||
await downloadFile(invalidDownloadLink, tempFolderPath, destinationFileName); | ||
} catch (err) { | ||
error = err; | ||
} | ||
expect(error?.name).to.equal('Error'); | ||
expect(error?.message).to.equal( | ||
'Failed to download file from remote path https://arweave.net/pVoSqZgJUCiNw7oS6CtlVEV8gREQlpRbccrsMLkeIuQ: Request failed with status code 404' | ||
); | ||
}); | ||
|
||
after(() => { | ||
cleanUpTempFolder(); | ||
}); | ||
}); |
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,50 @@ | ||
import * as fs from 'fs'; | ||
import path from 'path'; | ||
import axios from 'axios'; | ||
import util from 'util'; | ||
import stream from 'stream'; | ||
|
||
const pipeline = util.promisify(stream.pipeline); | ||
|
||
type DownloadProgressCallback = (downloadProgress: number) => void; | ||
type DownloadResult = { pathToFile: string; contentType: string }; | ||
|
||
/** | ||
* Downloads file from remote HTTP[S] host and puts its contents to the | ||
* specified location. | ||
* @param url URL of the file to download. | ||
* @param destinationPath Path to the destination file. | ||
* @param destinationFileName The file name. | ||
*/ | ||
|
||
export async function downloadFile( | ||
url: string, | ||
destinationPath: string, | ||
destinationFileName: string, | ||
downloadProgressCallback?: DownloadProgressCallback | ||
): Promise<DownloadResult> { | ||
const pathToFile = path.join(destinationPath, destinationFileName); | ||
const writer = fs.createWriteStream(pathToFile); | ||
|
||
try { | ||
const { data, headers } = await axios({ | ||
method: 'get', | ||
url: url, | ||
responseType: 'stream' | ||
}); | ||
const totalLength = headers['content-length']; | ||
const contentType = headers['content-type']; | ||
let downloadedLength = 0; | ||
data.on('data', (chunk: string | unknown[]) => { | ||
downloadedLength += chunk.length; | ||
const downloadProgressPct = totalLength > 0 ? (downloadedLength / totalLength) * 100 : 0; | ||
|
||
downloadProgressCallback && downloadProgressCallback(downloadProgressPct); | ||
}); | ||
await pipeline(data, writer); | ||
return { pathToFile, contentType }; | ||
} catch (error) { | ||
writer.close(); | ||
throw new Error(`Failed to download file from remote path ${url}: ${error.message}`); | ||
} | ||
} |
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,3 @@ | ||
const ARDRIVE_PROGRESS_LOG = 'ARDRIVE_PROGRESS_LOG'; | ||
|
||
export const showProgressLog = process.env[ARDRIVE_PROGRESS_LOG] && process.env[ARDRIVE_PROGRESS_LOG] === '1'; |
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,44 @@ | ||
import { expect } from 'chai'; | ||
import { cleanUpTempFolder, getTempFolder } from './temp_folder'; | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
|
||
describe('temp folder functions', () => { | ||
describe('getTempFolder function', () => { | ||
it('returns a folder that exists', () => { | ||
const tempFolderPath = getTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(true); | ||
}); | ||
|
||
it('getTempFolder can be called twice in a row', () => { | ||
const tempFolderPath = getTempFolder(); | ||
const tempFolderPath2 = getTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(true); | ||
expect(fs.existsSync(tempFolderPath2)).to.equal(true); | ||
}); | ||
|
||
it('returns a folder that contains the correct subfolders', () => { | ||
const tempFolderPath = getTempFolder(); | ||
const expectedPathComponent = | ||
os.platform() === 'win32' ? '\\ardrive-downloads' : '/.ardrive/ardrive-downloads'; | ||
expect(tempFolderPath).to.contains(expectedPathComponent); | ||
}); | ||
}); | ||
|
||
describe('cleanUpTempFolder function', () => { | ||
it('cleanUpTempFolder removes the temporary folder from the local system', () => { | ||
const tempFolderPath = getTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(true); | ||
cleanUpTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(false); | ||
}); | ||
it('cleanUpTempFolder can be called twice in a row', () => { | ||
const tempFolderPath = getTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(true); | ||
cleanUpTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(false); | ||
cleanUpTempFolder(); | ||
expect(fs.existsSync(tempFolderPath)).to.equal(false); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.