-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed some bugs and updated readme (#69)
- Loading branch information
1 parent
73a68d9
commit 8506653
Showing
6 changed files
with
121 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import request from 'request-promise' | ||
import fetch from 'node-fetch' | ||
import getFile from './getFile.js' | ||
import fs from 'fs.js' | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
const token = process.env.CANVAS_API_TOKEN | ||
|
||
const requestObj = url => ({ | ||
'method': 'GET', | ||
'uri': url, | ||
'json': true, | ||
'resolveWithFullResponse': true, | ||
'headers': { | ||
'Authorization': 'Bearer ' + token | ||
} | ||
}) | ||
|
||
/** | ||
* Downloads specified fileID from Canvas to specified path | ||
* @param {Number} fileId the fileId of the file | ||
* @param {String} path the path that the file should be downloaded to | ||
* @return {Promise} A Promise that resolves to a log that inidicated what the filename of the downloaded file is. On error, logs the error | ||
* @param {String} downloadPath the path that the file should be downloaded to | ||
* @return {Promise} A Promise that resolves to a log that indicates what the filename of the downloaded file is. On error, logs the error | ||
*/ | ||
|
||
const downloadFile = (fileId, path) => | ||
getFile(fileId, path) | ||
.then(({ url, filename }) => { | ||
const modifiedName = filename.replace(/%28/g, '(').replace(/%29/g, ')').replace(/%E2%80%93/g, '-') | ||
const pdfStream = fs.createWriteStream(path + modifiedName) | ||
request(requestObj(url)).pipe(pdfStream) | ||
return new Promise((resolve, reject) => { | ||
pdfStream.on('finish', () => resolve(modifiedName)) | ||
pdfStream.on('error', err => reject(err)) | ||
}) | ||
const downloadFile = async (fileId, downloadPath) => { | ||
try { | ||
const { url, filename } = await getFile(fileId, downloadPath) | ||
const modifiedName = filename | ||
.replace(/%28/g, '(') | ||
.replace(/%29/g, ')') | ||
.replace(/%E2%80%93/g, '-') | ||
|
||
const fileStream = fs.createWriteStream(path.join(downloadPath, modifiedName)) | ||
|
||
const response = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
'Authorization': `Bearer ${token}` | ||
} | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to download file: ${response.statusText}`) | ||
} | ||
|
||
// Pipe the response body to the file stream | ||
response.body.pipe(fileStream) | ||
|
||
return new Promise((resolve, reject) => { | ||
fileStream.on('finish', () => resolve(modifiedName)) | ||
fileStream.on('error', err => reject(err)) | ||
}) | ||
} catch (error) { | ||
console.error(`Error downloading file: ${error.message}`) | ||
throw error // Rethrow the error after logging | ||
} | ||
} | ||
|
||
export default downloadFile |
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 |
---|---|---|
@@ -1,68 +1,68 @@ | ||
export { default as batchCopyCourseContent } from './batchCopyCourseContent' | ||
export { default as copyCourseContent } from './copyCourseContent' | ||
export { default as createCourse } from './createCourse' | ||
export { default as createCustomGradebookColumn } from './createCustomGradebookColumn' | ||
export { default as createUser } from './createUser' | ||
export { default as createUserCourseEnrollment } from './createUserCourseEnrollment' | ||
export { default as createUserSectionEnrollment } from './createUserSectionEnrollment' | ||
export { default as deleteAllCustomGradebookColumns } from './deleteAllCustomGradebookColumns' | ||
export { default as downloadFile } from './downloadFile' | ||
export { default as deleteCustomGradebookColumn } from './deleteCustomGradebookColumn' | ||
export { default as getAccountIds } from './getAccountIds' | ||
export { default as getAccounts } from './getAccounts' | ||
export { default as getAllCoursesInAccount } from './getAllCoursesInAccount' | ||
export { default as getAllCoursesInDept } from './getAllCoursesInDept' | ||
export { default as getAllCourseSyllabiInAccount } from './getAllCourseSyllabiInAccount' | ||
export { default as getAnalytics } from './getAnalytics' | ||
export { default as getAssignments } from './getAssignments' | ||
export { default as getCourses } from './getCourses' | ||
export { default as getCoursesByUser } from './getCoursesByUser' | ||
export { default as getCustomGradeBookColumns } from './getCustomGradeBookColumns' | ||
export { default as getDeptIdsInAccount } from './getDeptIdsInAccount' | ||
export { default as getProgress } from './getProgress' | ||
export { default as getSubaccounts } from './getSubaccounts' | ||
export { default as getSyllabusOfCourse } from './getSyllabusOfCourse' | ||
export { default as getUserPageViews } from './getUserPageViews' | ||
export { default as getUsersInAccount } from './getUsersInAccount' | ||
export { default as getUsersInCourse } from './getUsersInCourse' | ||
export { default as hideCustomGradebookColumn } from './hideCustomGradebookColumn' | ||
export { default as hideCustomGradebookColumnsByName } from './hideCustomGradebookColumnsByName' | ||
export { default as putStudentNumberInExistingCustomColumn } from './putStudentNumberInExistingCustomColumn' | ||
export { default as putStudentNumberInGradeColumn } from './putStudentNumberInGradeColumn' | ||
export { default as putStudentNumbersInGradebook } from './putStudentNumbersInGradebook' | ||
export { default as showCustomGradebookColumn } from './showCustomGradebookColumn' | ||
export { default as showCustomGradebookColumnsByName } from './showCustomGradebookColumnsByName' | ||
export { default as checkProgressStatus } from './checkProgressStatus' | ||
export { default as getOptions } from './internal/getOptions' | ||
export { default as getUser } from './getUser' | ||
export { default as getRubricsInCourse } from './getRubricsInCourse' | ||
export { default as getRubric } from './getRubric' | ||
export { default as getAssignmentSubmissions } from './getAssignmentSubmissions' | ||
export { default as getEnrollmentsInCourse } from './getEnrollmentsInCourse' | ||
export { default as getSections } from './getSections' | ||
export { default as getModules } from './getModules' | ||
export { default as getModuleItems } from './getModuleItems' | ||
export { default as getDiscussionTopics } from './getDiscussionTopics' | ||
export { default as getDiscussionTopic } from './getDiscussionTopic' | ||
export { default as getFullDiscussion } from './getFullDiscussion' | ||
export { default as getSelf } from './getSelf' | ||
export { default as getOutcome } from './getOutcome' | ||
export { default as getQuizSubmissions } from './getQuizSubmissions' | ||
export { default as getQuizSubmissionEvents } from './getQuizSubmissionEvents' | ||
export { default as getQuizQuestions } from './getQuizQuestions' | ||
export { default as getCourseAnalytics } from './getCourseAnalytics' | ||
export { default as getPlannerItemsByUser } from './getPlannerItemsByUser' | ||
export { default as getGroupsInCourse } from './getGroupsInCourse' | ||
export { default as getGroupDiscussionTopics } from './getGroupDiscussionTopics' | ||
export { default as getFullGroupDiscussion } from './getFullGroupDiscussion' | ||
export { default as getGroupDiscussionTopic } from './getGroupDiscussionTopic' | ||
export { default as getQuizSubmission } from './getQuizSubmission' | ||
export { default as postAssignmentSubmissionComment } from './postAssignmentSubmissionComment' | ||
export { default as deleteSubmissionComment } from './deleteSubmissionComment' | ||
export { default as getCourse } from './getCourse' | ||
export { default as getCourseSections } from './getCourseSections' | ||
export { default as getEnrollmentsInSection } from './getEnrollmentsInSection' | ||
export { default as createGroup } from './createGroup' | ||
export { default as createGroupCategories } from './createGroupCategories' | ||
export { default as createGroupMembership } from './createGroupMembership' | ||
export { default as getHistory } from './getHistory' | ||
export { default as batchCopyCourseContent } from './batchCopyCourseContent.js' | ||
export { default as copyCourseContent } from './copyCourseContent.js' | ||
export { default as createCourse } from './createCourse.js' | ||
export { default as createCustomGradebookColumn } from './createCustomGradebookColumn.js' | ||
export { default as createUser } from './createUser.js' | ||
export { default as createUserCourseEnrollment } from './createUserCourseEnrollment.js' | ||
export { default as createUserSectionEnrollment } from './createUserSectionEnrollment.js' | ||
export { default as deleteAllCustomGradebookColumns } from './deleteAllCustomGradebookColumns.js' | ||
export { default as downloadFile } from './downloadFile.js' | ||
export { default as deleteCustomGradebookColumn } from './deleteCustomGradebookColumn.js' | ||
export { default as getAccountIds } from './getAccountIds.js' | ||
export { default as getAccounts } from './getAccounts.js' | ||
export { default as getAllCoursesInAccount } from './getAllCoursesInAccount.js' | ||
export { default as getAllCoursesInDept } from './getAllCoursesInDept.js' | ||
export { default as getAllCourseSyllabiInAccount } from './getAllCourseSyllabiInAccount.js' | ||
export { default as getAnalytics } from './getAnalytics.js' | ||
export { default as getAssignments } from './getAssignments.js' | ||
export { default as getCourses } from './getCourses.js' | ||
export { default as getCoursesByUser } from './getCoursesByUser.js' | ||
export { default as getCustomGradeBookColumns } from './getCustomGradeBookColumns.js' | ||
export { default as getDeptIdsInAccount } from './getDeptIdsInAccount.js' | ||
export { default as getProgress } from './getProgress.js' | ||
export { default as getSubaccounts } from './getSubaccounts.js' | ||
export { default as getSyllabusOfCourse } from './getSyllabusOfCourse.js' | ||
export { default as getUserPageViews } from './getUserPageViews.js' | ||
export { default as getUsersInAccount } from './getUsersInAccount.js' | ||
export { default as getUsersInCourse } from './getUsersInCourse.js' | ||
export { default as hideCustomGradebookColumn } from './hideCustomGradebookColumn.js' | ||
export { default as hideCustomGradebookColumnsByName } from './hideCustomGradebookColumnsByName.js' | ||
export { default as putStudentNumberInExistingCustomColumn } from './putStudentNumberInExistingCustomColumn.js' | ||
export { default as putStudentNumberInGradeColumn } from './putStudentNumberInGradeColumn.js' | ||
export { default as putStudentNumbersInGradebook } from './putStudentNumbersInGradebook.js' | ||
export { default as showCustomGradebookColumn } from './showCustomGradebookColumn.js' | ||
export { default as showCustomGradebookColumnsByName } from './showCustomGradebookColumnsByName.js' | ||
export { default as checkProgressStatus } from './checkProgressStatus.js' | ||
export { default as getOptions } from './internal/getOptions.js' | ||
export { default as getUser } from './getUser.js' | ||
export { default as getRubricsInCourse } from './getRubricsInCourse.js' | ||
export { default as getRubric } from './getRubric.js' | ||
export { default as getAssignmentSubmissions } from './getAssignmentSubmissions.js' | ||
export { default as getEnrollmentsInCourse } from './getEnrollmentsInCourse.js' | ||
export { default as getSections } from './getSections.js' | ||
export { default as getModules } from './getModules.js' | ||
export { default as getModuleItems } from './getModuleItems.js' | ||
export { default as getDiscussionTopics } from './getDiscussionTopics.js' | ||
export { default as getDiscussionTopic } from './getDiscussionTopic.js' | ||
export { default as getFullDiscussion } from './getFullDiscussion.js' | ||
export { default as getSelf } from './getSelf.js' | ||
export { default as getOutcome } from './getOutcome.js' | ||
export { default as getQuizSubmissions } from './getQuizSubmissions.js' | ||
export { default as getQuizSubmissionEvents } from './getQuizSubmissionEvents.js' | ||
export { default as getQuizQuestions } from './getQuizQuestions.js' | ||
export { default as getCourseAnalytics } from './getCourseAnalytics.js' | ||
export { default as getPlannerItemsByUser } from './getPlannerItemsByUser.js' | ||
export { default as getGroupsInCourse } from './getGroupsInCourse.js' | ||
export { default as getGroupDiscussionTopics } from './getGroupDiscussionTopics.js' | ||
export { default as getFullGroupDiscussion } from './getFullGroupDiscussion.js' | ||
export { default as getGroupDiscussionTopic } from './getGroupDiscussionTopic.js' | ||
export { default as getQuizSubmission } from './getQuizSubmission.js' | ||
export { default as postAssignmentSubmissionComment } from './postAssignmentSubmissionComment.js' | ||
export { default as deleteSubmissionComment } from './deleteSubmissionComment.js' | ||
export { default as getCourse } from './getCourse.js' | ||
export { default as getCourseSections } from './getCourseSections.js' | ||
export { default as getEnrollmentsInSection } from './getEnrollmentsInSection.js' | ||
export { default as createGroup } from './createGroup.js' | ||
export { default as createGroupCategories } from './createGroupCategories.js' | ||
export { default as createGroupMembership } from './createGroupMembership.js' | ||
export { default as getHistory } from './getHistory.js' |