Added Metadata to Upload API routes

This commit is contained in:
Jyotirmoy Bandyopadhayaya 2023-06-24 13:10:39 +05:30
parent bdbca19eba
commit 5e5bbc2bed
Signed by: bravo68web
GPG Key ID: F5671FD7BCB9917A
5 changed files with 77 additions and 11 deletions

View File

@ -125,9 +125,21 @@ export default class UploadController
next: NextFunction
): Promise<Response | void> => {
try {
const { limit, offset, query = '' } = req.query;
const data = await this.listFilesS(query, Number(limit), Number(offset));
res.status(200).json(makeResponse(data));
const {
limit,
page,
query = '',
} = req.query as {
limit: string;
page: string;
query: string;
};
const { data, meta } = await this.listFilesS(
query,
parseInt(limit),
parseInt(page)
);
res.status(200).json(makeResponse(data, meta));
} catch (error: any) {
next(error);
}

View File

@ -52,10 +52,20 @@ export interface IUploaderService {
searchQuery: any,
limit: number,
offset: number
): Promise<Uploads[]>;
): Promise<IListUploads>;
getFileS(fileID: string): Promise<Uploads>;
}
export interface IListUploads {
data: Uploads[];
meta: {
total: number;
pageNo: number;
pageSize: number;
totalPages: number;
};
}
export interface UploaderConfig {
mimeFilters: string[];
}

View File

@ -5,6 +5,7 @@ import APIKeyAuth from '../middlewares/apikey_check';
import {
deleteFileValidation,
urlUploadValidation,
listFileValidation,
} from '../validators/upload.validation';
const uploadController = new UploadController();
@ -45,7 +46,12 @@ router.post(
uploadController.uploadFileFromURL as any
);
router.get('/', apiKeyAuth.check as any, uploadController.getAllFiles as any);
router.get(
'/',
apiKeyAuth.check as any,
listFileValidation as any,
uploadController.getAllFiles as any
);
router.get(
'/:fileID',

View File

@ -2,7 +2,7 @@ import UploaderService from '../data/uploader.service';
import { gql } from 'graphql-request';
import { client } from '../helpers';
import sharp from 'sharp';
import { IUploaderService } from '../interfaces/upload.interface';
import { IListUploads, IUploaderService } from '../interfaces/upload.interface';
import { UserMeta } from '../types';
import axios from 'axios';
import fs from 'fs';
@ -333,9 +333,9 @@ export default class Uploader implements IUploaderService {
public listFilesS = async (
searchQuery: any,
limit: number,
offset: number
): Promise<Uploads[]> => {
limit = 1,
offset = 10
): Promise<IListUploads> => {
const query = gql`
query listFiles($searchQuery: String!, $limit: Int!, $offset: Int!) {
uploads(
@ -373,14 +373,27 @@ export default class Uploader implements IUploaderService {
const variables = {
searchQuery,
limit: limit,
offset: offset,
offset: (offset - 1) * limit,
};
const data: {
uploads: Uploads[];
uploads_aggregate: {
aggregate: {
count: number;
};
};
} = await client.request(query, variables);
return data.uploads;
return {
data: data.uploads,
meta: {
total: data.uploads_aggregate.aggregate.count,
pageNo: offset,
pageSize: limit,
totalPages: Math.ceil(data.uploads_aggregate.aggregate.count / limit),
},
};
};
public getFileS = async (fileID: string): Promise<Uploads> => {

View File

@ -49,3 +49,28 @@ export const urlUploadValidation = async (
return;
}
};
export const listFileValidation = async (
req: ModRequest,
res: Response,
next: NextFunction
) => {
try {
const schema = Joi.object().keys({
query: Joi.string().optional(),
page: Joi.number().optional().default(1),
limit: Joi.number().optional().default(10),
});
req.query = await schema.validateAsync(req.query);
next();
} catch (err) {
res.status(400).send(
new CustomError({
data: err,
message: ErrorMsg.VALIDATION,
statusCode: 400,
})
);
return;
}
};