-
-
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.
- Loading branch information
1 parent
ec309af
commit 062e3a4
Showing
5 changed files
with
81 additions
and
18 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,13 @@ | ||
/** | ||
* Returns a JavaScript Response object containing the given data. | ||
* | ||
* @param {*} data - Data that will be included in the response | ||
*/ | ||
export function JSONResponse(data) { | ||
const headers = new Headers() | ||
headers.set('Content-Type', 'application/json') | ||
return new Response( | ||
JSON.stringify(data), | ||
{headers} | ||
) | ||
} |
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,46 @@ | ||
// Utils | ||
import {JSONResponse} from '../lib/utils' | ||
|
||
// Stores | ||
import stores from '../stores' | ||
|
||
/** | ||
* Handler for the /api/tree/:path requests. | ||
* | ||
* The GET method returns the list of files in the folder. | ||
* The POST method is not yet implemented; eventually, it will allow uploading files. | ||
* | ||
* @param {Request} req - Request object from the client | ||
* @returns {Response} Response object for the request | ||
*/ | ||
export default async function(req) { | ||
// Get the method of the request | ||
// Only GET requests are implemented for now | ||
const method = req.method | ||
if (method != 'GET') { | ||
throw Error('Invalid request method') | ||
} | ||
|
||
// Ensure we have the master key | ||
if (!stores.masterKey || !stores.index) { | ||
throw Error('Repository is not unlocked') | ||
} | ||
|
||
// Get the file path from the URL | ||
const reqPath = (new URL(req.url)).pathname | ||
if (!reqPath || !reqPath.startsWith('/api/tree')) { | ||
throw Error('Invalid request path') | ||
} | ||
let path = reqPath.substr(9) || '/' | ||
|
||
// Ensure path starts with / | ||
if (path.charAt(0) != '/') { | ||
path = '/' + path | ||
} | ||
|
||
// Get the list of files | ||
const list = await stores.index.listFolder(path) || [] | ||
|
||
// Return a Response object with the list of files | ||
return JSONResponse(list) | ||
} |