-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Fetch, Parse, and Create Documents for Statically Hosted Files #4398
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e658700
Add capability to web scraping feature for document creation to downlβ¦
angelplusultra de1793a
lint
angelplusultra d4a656f
Remove unneeded comment
angelplusultra 4acb31d
Simplified process by using key of ACCEPTED_MIMES to validate the resβ¦
angelplusultra 3c61e40
Add TODO comments for future implementation of asDoc.js to handle staβ¦
angelplusultra f882796
Return captureAs argument to be exposed by scrapeGenericUrl and passeβ¦
angelplusultra d033ad4
Return debug log for scrapeGenericUrl
angelplusultra 2945c26
Change conditional to a guard clause.
angelplusultra 44f45b4
Add error handling, validation, and JSDOC to getContentType helper fn
angelplusultra c4d67ac
remove unneeded comments
angelplusultra d996b60
Simplify URL validation by reusing module
angelplusultra 0bba2c7
Rename downloadFileToHotDir to downloadURIToFile and moved up to a glβ¦
angelplusultra e3dad4e
Merge branch 'master' into 2110-download-file-as-document
angelplusultra ffee0eb
refactor
timothycarambat 211ecd6
add support for webp
timothycarambat 6872905
Merge branch 'master' into 2110-download-file-as-document
timothycarambat 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,72 @@ | ||
| const { validURL } = require("../../utils/url"); | ||
|
|
||
| /** | ||
| * Get the content type of a resource | ||
| * - Sends a HEAD request to the URL and returns the Content-Type header with a 5 second timeout | ||
| * @param {string} url - The URL to get the content type of | ||
| * @returns {Promise<{success: boolean, reason: string|null, contentType: string|null}>} - The content type of the resource | ||
| */ | ||
| async function getContentTypeFromURL(url) { | ||
| try { | ||
| if (!url || typeof url !== "string" || !validURL(url)) | ||
| return { success: false, reason: "Not a valid URL.", contentType: null }; | ||
|
|
||
| const abortController = new AbortController(); | ||
| const timeout = setTimeout(() => { | ||
| abortController.abort(); | ||
| console.error("Timeout fetching content type for URL:", url.toString()); | ||
| }, 5_000); | ||
|
|
||
| const res = await fetch(url, { | ||
| method: "HEAD", | ||
| signal: abortController.signal, | ||
| }).finally(() => clearTimeout(timeout)); | ||
|
|
||
| if (!res.ok) | ||
| return { | ||
| success: false, | ||
| reason: `HTTP ${res.status}: ${res.statusText}`, | ||
| contentType: null, | ||
| }; | ||
|
|
||
| const contentType = res.headers.get("Content-Type")?.toLowerCase(); | ||
| const contentTypeWithoutCharset = contentType?.split(";")[0].trim(); | ||
| if (!contentTypeWithoutCharset) | ||
| return { | ||
| success: false, | ||
| reason: "No Content-Type found.", | ||
| contentType: null, | ||
| }; | ||
| return { | ||
| success: true, | ||
| reason: null, | ||
| contentType: contentTypeWithoutCharset, | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| success: false, | ||
| reason: `Error: ${error.message}`, | ||
| contentType: null, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function returnResult({ | ||
| success, | ||
| reason, | ||
| documents, | ||
| content, | ||
| saveAsDocument = true, | ||
| }) { | ||
| if (!saveAsDocument) { | ||
| return { | ||
| success, | ||
| content, | ||
| }; | ||
| } else return { success, reason, documents }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| returnResult, | ||
| getContentTypeFromURL, | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
| const { WATCH_DIRECTORY } = require("../constants"); | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const { pipeline } = require("stream/promises"); | ||
| const { validURL } = require("../url"); | ||
|
|
||
| /** | ||
| * Download a file to the hotdir | ||
| * @param {string} url - The URL of the file to download | ||
| * @param {number} maxTimeout - The maximum timeout in milliseconds | ||
| * @returns {Promise<{success: boolean, fileLocation: string|null, reason: string|null}>} - The path to the downloaded file | ||
| */ | ||
| async function downloadURIToFile(url, maxTimeout = 10_000) { | ||
| if (!url || typeof url !== "string" || !validURL(url)) | ||
| return { success: false, reason: "Not a valid URL.", fileLocation: null }; | ||
|
|
||
| try { | ||
| const abortController = new AbortController(); | ||
| const timeout = setTimeout(() => { | ||
| abortController.abort(); | ||
| console.error( | ||
| `Timeout ${maxTimeout}ms reached while downloading file for URL:`, | ||
| url.toString() | ||
| ); | ||
| }, maxTimeout); | ||
|
|
||
| const res = await fetch(url, { signal: abortController.signal }) | ||
| .then((res) => { | ||
| if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`); | ||
| return res; | ||
| }) | ||
| .finally(() => clearTimeout(timeout)); | ||
|
|
||
| const localFilePath = path.join(WATCH_DIRECTORY, path.basename(url)); | ||
| const writeStream = fs.createWriteStream(localFilePath); | ||
| await pipeline(res.body, writeStream); | ||
|
|
||
| console.log(`[SUCCESS]: File ${localFilePath} downloaded to hotdir.`); | ||
| return { success: true, fileLocation: localFilePath, reason: null }; | ||
| } catch (error) { | ||
| console.error(`Error writing to hotdir: ${error} for URL: ${url}`); | ||
| return { success: false, reason: error.message, fileLocation: null }; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| downloadURIToFile, | ||
| }; |
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.