This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Easy way to download attachments on studenti.it
- Loading branch information
Showing
2 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 rospino74 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
export default async function getFileDownloadUrl(documentId: string): Promise<string> { | ||
const apiUrl = `https://doc.studenti.it/ajax/download.php`; | ||
const body = `k=${documentId}`; | ||
|
||
// Post request to get the download url | ||
const response = await fetch(apiUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': body.length.toString(), | ||
}, | ||
body | ||
}); | ||
|
||
// Watch out for http errors | ||
if (!response.ok) { | ||
throw new Error(`${response.status} ${response.statusText}`); | ||
} | ||
|
||
const json = await response.json(); | ||
|
||
// Response object | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(json) | ||
} | ||
|
||
if (json.esito != 'OK') { | ||
throw new Error(`${response.status} ${response.statusText}: ${json.messaggio}`); | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
if (/^it\b/.test(navigator.language)) { | ||
console.log('%cIndirizzo del download: %c%s', 'color: #7ab700;', 'color: gray;', json.link); | ||
} else { | ||
console.log('%cDownload url: %c%s', 'color: #7ab700;', 'color: gray;', json.link); | ||
} | ||
} | ||
|
||
return json.link; | ||
} |
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
960fd1f
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.
why tf is this "comitted" in 2004?