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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 18x 18x 18x 18x 18x 18x 18x 34x 34x 34x 34x 14x 14x 14x 14x 6x 6x 9x 9x 5x 4x 4x 4x 4x 4x 4x 3x 1x 14x 18x 18x 18x | /** * Image service. * @packageDocumentation */ import { Injectable, Inject, forwardRef } from '@nestjs/common'; import { DBService } from '../db/db.service'; import { ResourceService } from '../resource/resource.service'; import { NBImage } from './image.interface'; import { ServiceException } from '../service.exception'; import * as jimp from 'jimp'; /** * Image table name. */ export const imageTableName = 'NB_IMAGE'; /** * Image table service. */ @Injectable() export class ImageService { constructor( @Inject(forwardRef(() => DBService)) private readonly dbService: DBService, @Inject(forwardRef(() => ResourceService)) private readonly resourceService: ResourceService, ) {} /** * Create a new image in the database. * * @param data The image data. * @returns The new image record. */ public async createImage(data: string): Promise<NBImage> { const imageData = await this.shrinkImageAutoBase64(data); const maxImageSize = await this.resourceService.getResource<number>( 'MAX_IMAGE_SIZE', ); Eif (imageData.length < maxImageSize) { return this.dbService.create<NBImage>(imageTableName, { data: imageData, }); } else { throw new ServiceException('Image is too large'); } } /** * Determine whether or not an image exists. * * @param imageID The ID of the image. * @returns Whether or not the image exists. */ public async imageExists(imageID: string): Promise<boolean> { const image = await this.dbService.getByID<NBImage>( imageTableName, imageID, ); return !!image; } /** * Get an image. * * @param imageID The ID of the image. * @returns The image record. */ public async getImage(imageID: string): Promise<NBImage> { const image = await this.dbService.getByID<NBImage>( imageTableName, imageID, ); if (image) { return image; } else { throw new ServiceException('Image does not exist'); } } /** * Set an image's data. * * @param imageID The ID of the image. * @param newData The new image data. * @returns The updated image record. */ public async setImageData( imageID: string, newData: string, ): Promise<NBImage> { const imageData = await this.shrinkImageAutoBase64(newData); const maxImageSize = await this.resourceService.getResource<number>( 'MAX_IMAGE_SIZE', ); Eif (imageData.length < maxImageSize) { const imageExists = await this.imageExists(imageID); if (imageExists) { return this.dbService.updateByID<NBImage>(imageTableName, imageID, { data: imageData, }); } else { throw new ServiceException('Image does not exist'); } } else { throw new ServiceException('Image is too large'); } } /** * Delete an image. * * @param imageID The ID of the image. */ public async deleteImage(imageID: string): Promise<void> { await this.dbService.deleteByID(imageTableName, imageID); } /** * Shrink an image. * * @param image The image buffer. * @param factor The scale factor. * @param quality The JPEG quality. * @returns The resulting image buffer. */ private async shrinkImage( image: Buffer, factor: number, quality = 100, ): Promise<Buffer> { const img = await jimp.read(image); const width = img.bitmap.width; return img .resize(Math.floor(width * factor), jimp.AUTO) .quality(quality) .getBufferAsync(jimp.MIME_JPEG); } /** * Shrink an image to a maximum size. * * @param image The image buffer. * @param maxWidth The maximum width of the image. * @param maxHeight The maximum height of the image. * @returns The resulting image buffer. */ private async shrinkImageToSize( image: Buffer, maxWidth = 1920, maxHeight = 1080, ): Promise<Buffer> { const img = await jimp.read(image); const existingRatio = img.bitmap.width / img.bitmap.height; const potentialRatio = maxWidth / maxHeight; if (existingRatio > potentialRatio) { return img.resize(maxWidth, jimp.AUTO).getBufferAsync(jimp.MIME_JPEG); } else { return img.resize(jimp.AUTO, maxHeight).getBufferAsync(jimp.MIME_JPEG); } } /** * Shrink an image automatically. * * @param image The image buffer. * @param maxImageSize The maximum size of the image. * @param maxWidth The maximum width of the image. * @param maxHeight The maximum height of the image. * @param factor The scale factor. * @param quality The JPEG quality. * @returns The resulting image buffer. */ private async shrinkImageAuto( image: Buffer, maxImageSize: number, maxWidth = 1600, maxHeight = 900, factor = 0.7071, quality = 40, ): Promise<Buffer> { if (image.length < maxImageSize) { return image; } else { const buffer = await this.shrinkImageToSize(image, maxWidth, maxHeight); if (buffer.length < maxImageSize) { return buffer; } else { let newBuffer = await this.shrinkImage(buffer, factor, quality); while (newBuffer.length >= maxImageSize) { newBuffer = await this.shrinkImage(newBuffer, factor, quality); } return newBuffer; } } } /** * Shrinks a base64 encoded image automatically. * * @param imageB64 The base64 image. * @param maxWidth The maximum width of the image. * @param maxHeight The maximum height of the image. * @param factor The scale factor. * @param quality The JPEG quality. * @returns The shrunk base64 image. */ private async shrinkImageAutoBase64( imageB64: string, maxWidth = 1600, maxHeight = 900, factor = 0.7071, quality = 40, ): Promise<string> { const maxImageSize = await this.resourceService.getResource<number>( 'MAX_IMAGE_SIZE', ); Eif (imageB64.length < maxImageSize) { return imageB64; } else { const imageBuffer = Buffer.from(imageB64, 'base64'); let shrunkImageBuffer = await this.shrinkImageToSize( imageBuffer, maxWidth, maxHeight, ); let shrunkImageB64 = shrunkImageBuffer.toString('base64'); while (shrunkImageB64.length >= maxImageSize) { shrunkImageBuffer = await this.shrinkImage( shrunkImageBuffer, factor, quality, ); shrunkImageB64 = shrunkImageBuffer.toString('base64'); } return shrunkImageB64; } } } |