-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement custom download API and related functionality for markdown …
…files
- Loading branch information
Showing
4 changed files
with
55 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
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,18 @@ | ||
{ | ||
"name": "Custom Download", | ||
"version": "1.0.0", | ||
"guard": "-", | ||
"paths": [ | ||
{ | ||
"path": "/*name", | ||
"method": "GET", | ||
"process": "scripts.download.Markdown", | ||
"in": ["$param.name"], | ||
"out": { | ||
"status": 200, | ||
"headers": { "Content-Type": "{{ type }}" }, | ||
"body": "{{ content }}" | ||
} | ||
} | ||
] | ||
} |
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,23 @@ | ||
import { Exception, FS } from "@yao/runtime"; | ||
|
||
/** | ||
* Download markdown file by name | ||
* yao run scripts.download.Markdown README.md | ||
* curl -vv http://localhost:5099/api/download/README.md | ||
* @param name | ||
*/ | ||
function Markdown(name: string) { | ||
const fs = new FS(`app`); // app is the application root directory | ||
// const fs = new FS("data"); // data is the application data directory | ||
const filename = `${name}`; | ||
const ext = fs.ExtName(filename); | ||
if (ext !== "md") { | ||
throw new Exception(`File is not markdown: ${filename}`, 400); | ||
} | ||
|
||
if (!fs.Exists(filename)) { | ||
throw new Exception(`File not found: ${filename}`, 404); | ||
} | ||
|
||
return fs.Download(filename); | ||
} |
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