Require specifying PUT/POST type on /:slug

This commit is contained in:
Alice Gaudon 2020-07-05 19:43:56 +02:00
parent 7dbe9192cd
commit f7a2603e7f
4 changed files with 21 additions and 13 deletions

View File

@ -6,12 +6,14 @@ token=$(cat $HOME/.ily_token)
if test -z "$2"; then if test -z "$2"; then
curl -X POST -H 'Accept: text/plain' \ curl -X POST -H 'Accept: text/plain' \
-H "Authorization: $token" \ -H "Authorization: $token" \
-F 'type=file' \
-F 'upload=@'$local_file \ -F 'upload=@'$local_file \
-F 'ttl=2592000' \ -F 'ttl=2592000' \
https://ily.li/ https://ily.li/
else else
curl -X PUT -H 'Accept: text/plain' \ curl -X PUT -H 'Accept: text/plain' \
-H "Authorization: $token" \ -H "Authorization: $token" \
-F 'type=file' \
-F 'upload=@'$local_file \ -F 'upload=@'$local_file \
-F 'ttl=2592000' \ -F 'ttl=2592000' \
"https://ily.li/$2" "https://ily.li/$2"

View File

@ -86,11 +86,14 @@ export default class FileController extends Controller {
} }
} }
protected async postFile(req: Request, res: Response): Promise<void> { protected async postFile(req: Request, res: Response, next: NextFunction): Promise<void> {
if (req.body.type !== 'file') return next();
await this.handleFileUpload(req.body.slug || await this.generateSlug(10), req, res); await this.handleFileUpload(req.body.slug || await this.generateSlug(10), req, res);
} }
protected async putFile(req: Request, res: Response): Promise<void> { protected async putFile(req: Request, res: Response, next: NextFunction): Promise<void> {
if (req.body.type !== 'file') return next();
const slug = req.params.slug; const slug = req.params.slug;
if (!slug) throw new BadRequestError('Cannot put without a slug.', 'Either provide a slug or use POST method instead.', req.url); if (!slug) throw new BadRequestError('Cannot put without a slug.', 'Either provide a slug or use POST method instead.', req.url);

View File

@ -15,7 +15,7 @@ export default class FileModel extends Model {
} }
public static async paginateForUser(req: Request, perPage: number, user_id: number): Promise<FileModel[]> { public static async paginateForUser(req: Request, perPage: number, user_id: number): Promise<FileModel[]> {
return await this.paginate<FileModel>(req, perPage, this.select().where('user_id', user_id)); return await this.paginate(req, perPage, this.select().where('user_id', user_id));
} }
public readonly user_id!: number; public readonly user_id!: number;

View File

@ -100,14 +100,17 @@
<p>Example with curl:</p> <p>Example with curl:</p>
<pre>curl -X POST -H 'Accept: application/json' \ <pre>curl -X POST -H 'Accept: application/json' \
-H "Authorization: very_secret_and_personal_token" \ -H "Authorization: very_secret_and_personal_token" \
-F 'type=file' \
-F 'upload=@path/to/local/file' \ -F 'upload=@path/to/local/file' \
https://ily.li/</pre> https://ily.li/</pre>
<pre>curl -X PUT -H 'Accept: application/json' \ <pre>curl -X PUT -H 'Accept: application/json' \
-H "Authorization: very_secret_and_personal_token" \ -H "Authorization: very_secret_and_personal_token" \
-F 'type=file' \
-F 'upload=@path/to/local/file' \ -F 'upload=@path/to/local/file' \
https://ily.li/my_very_important_file.png</pre> https://ily.li/my_very_important_file.png</pre>
<pre>curl -X POST -H 'Accept: application/json' \ <pre>curl -X POST -H 'Accept: application/json' \
-H "Authorization: very_secret_and_personal_token" \ -H "Authorization: very_secret_and_personal_token" \
-F 'type=file' \
-F 'upload=@path/to/local/file' \ -F 'upload=@path/to/local/file' \
-F 'ttl=30' \ -F 'ttl=30' \
https://ily.li/</pre> https://ily.li/</pre>