Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 4x 4x 4x 3x 1x 1x 1x 4x 4x 4x 4x 4x 4x 3x 4x 4x 3x 1x 2x 2x 1x | 'use strict';
const namespace = use('Config').get('modules.media.general.namespace');
const Logger = make('Adonis/Src/Logger');
const mime = require('mime-types');
const { md5 } = use('C2C/Helpers');
const { NotFoundException, BadRequestException } = use('C2C/Exceptions');
class MediaController {
static get inject() {
return [
'C2C/Services/AwsService',
`${namespace}/Services/locationService`,
`${namespace}/Services/SalonService`,
];
}
constructor(awsService, locationService, salonService) {
this.awsService = awsService;
this.locationService = locationService;
this.salonService = salonService;
this.logger = Logger;
}
/**
* @swagger
*
* /api/v1/media/images/signature:
* post:
* tags:
* - media
* description: get signature key to upload image to store
* summary: 'getImageSignatureKey'
* operationId: 'getImageSignatureKey'
* security:
* - BearerAuth: []
* - BasicAuth: []
* requestBody:
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ImageSignatureRequestBody'
* responses:
* 200:
* description: success
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ImageSignatureResponse'
* 404:
* description: Salon not found
* content:
* application/json:
* schema:
* type: object
* properties:
* statusCode:
* type: number
* example: 404
* error:
* type: string
* example: NotFoundException
* message:
* type: string
* example: Salon not found
* data:
* type: array
* example: []
* items:
* type: object
* 422:
* description: unprocessable entity
* content:
* application/json:
* schema:
* type: object
* properties:
* statusCode:
* type: number
* example: 422
* error:
* type: string
* example: ValidationException
* message:
* type: string
* example: The image type should be gif, jpeg, webp, bmp
* data:
* type: array
* items:
* type: object
* properties:
* message:
* type: string
* field:
* type: string
* example: email
* validation:
* type: string
*/
async getSignature({ request, response, antl }) {
const { fileName, salonId = '' } = request.all();
let imagePath = '';
if (request.user && request.user.userId) {
// On the ROP flow, take auth.userId ( operator id ) as path
imagePath = `images/${request.user.userId}`;
} else Eif (salonId) {
// On the M2M flow, take salon.operatorId as path
const salon = await this.salonService.findOrFail(salonId);
imagePath = `images/${salon.operatorId}`;
} else {
throw NotFoundException.raise('errors.salonNotFound');
}
// The file name on S3 must be hashed to avoid special character handling
// Refer https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
const splitFileName = fileName.split('.');
// Should ensure the filename contains an extension. The validator has already verified it.
Iif (splitFileName.length <= 1) throw BadRequestException.raise('errors.invalidParam');
const extension = splitFileName.pop();
const hashedFileName = md5(splitFileName.join('.'));
const key = `${imagePath}/${Date.now()}_${hashedFileName}.${extension}`;
const data = await this.awsService.getSignedUrl({
key,
contentType: mime.lookup(fileName),
});
return response.success({
signed: data,
url: this.awsService.replaceCloudFrontUrl(data.split('?')[0]),
});
}
/**
* @swagger
* /api/v1/media/address:
* post:
* tags:
* - media
* summary: 'getAddressByPostCode'
* operationId: 'getAddressByPostCode'
* security:
* - BearerAuth: []
* - BasicAuth: []
* description: 'get address by post code'
* requestBody:
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/GetAddressByPostCodeRequestBody'
* responses:
* 200:
* description: success
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/GetAddressByPostCodeResponse'
* 404:
* description: Location of post code not found
* content:
* application/json:
* schema:
* type: object
* properties:
* statusCode:
* type: number
* example: 404
* error:
* type: string
* example: NotFoundException
* message:
* type: string
* example: Location of post code not found
* data:
* type: array
* example: []
* items:
* type: object
* 422:
* description: unprocessable entity
* content:
* application/json:
* schema:
* type: object
* properties:
* statusCode:
* type: number
* example: 422
* error:
* type: string
* example: ValidationException
* message:
* type: string
* example: Invalid data
* data:
* type: array
* items:
* type: object
* properties:
* message:
* type: string
* example: Please provide a valid PostCode
* field:
* type: string
* example: postCode
* validation:
* type: string
*/
async getAddress({ request, response, antl, transform }) {
const { postCode } = request.post();
const location = await this.locationService.getLocationByPostCode(postCode);
if (!location) {
throw new NotFoundException(antl.get(`errors.postCodeNotFound`));
}
const transformed = await transform.item(location, 'LocationTransformer');
return response.success(transformed);
}
}
module.exports = MediaController;
|