|
| 1 | +const axios = require('axios'); |
| 2 | +const uuid = require('uuid'); |
| 3 | + |
| 4 | +const REQUEST_TIMEOUT = process.env.REQUEST_TIMEOUT ? parseInt(process.env.REQUEST_TIMEOUT, 10) : 10000; // 10s |
| 5 | +const REQUEST_MAX_RETRY = process.env.REQUEST_MAX_RETRY ? parseInt(process.env.REQUEST_MAX_RETRY, 10) : 7; // 10s |
| 6 | +const REQUEST_RETRY_DELAY = process.env.REQUEST_RETRY_DELAY ? parseInt(process.env.REQUEST_RETRY_DELAY, 10) : 7000; // 7s |
| 7 | +const REQUEST_MAX_CONTENT_LENGTH = process.env.REQUEST_MAX_CONTENT_LENGTH ? parseInt(process.env.REQUEST_MAX_CONTENT_LENGTH, 10) : 10485760; // 10MB |
| 8 | +const { ATTACHMENT_STORAGE_SERVICE_BASE_URL } = process.env; |
| 9 | + |
| 10 | +// Adapted from https://github.com/elasticio/component-commons-library/blob/master/lib/attachment/AttachmentProcessor.ts |
| 11 | +class AttachmentProcessor { |
| 12 | + constructor(emitter, token, attachmentStorageServiceBaseUrl) { |
| 13 | + this.attachmentService = attachmentStorageServiceBaseUrl; |
| 14 | + this.emitter = emitter; |
| 15 | + this.token = token; |
| 16 | + } |
| 17 | + |
| 18 | + async getAttachment(url, responseType) { |
| 19 | + const ax = axios.create(); |
| 20 | + AttachmentProcessor.addRetryCountInterceptorToAxios(ax); |
| 21 | + |
| 22 | + console.log(`Getting attachment ${responseType} from ${url}`); |
| 23 | + const axConfig = { |
| 24 | + url, |
| 25 | + responseType, |
| 26 | + method: 'get', |
| 27 | + timeout: REQUEST_TIMEOUT, |
| 28 | + retry: REQUEST_MAX_RETRY, |
| 29 | + delay: REQUEST_RETRY_DELAY, |
| 30 | + withCredentials: true, |
| 31 | + headers: { |
| 32 | + Authorization: `Bearer ${this.token}`, |
| 33 | + }, |
| 34 | + }; |
| 35 | + |
| 36 | + return ax(axConfig); |
| 37 | + } |
| 38 | + |
| 39 | + async uploadAttachment(body, mimeType) { |
| 40 | + const putUrl = await AttachmentProcessor.preparePutUrl(this.attachmentService); |
| 41 | + const ax = axios.create(); |
| 42 | + AttachmentProcessor.addRetryCountInterceptorToAxios(ax); |
| 43 | + |
| 44 | + const axConfig = { |
| 45 | + url: putUrl, |
| 46 | + data: body, |
| 47 | + method: 'put', |
| 48 | + timeout: REQUEST_TIMEOUT, |
| 49 | + retry: REQUEST_MAX_RETRY, |
| 50 | + delay: REQUEST_RETRY_DELAY, |
| 51 | + maxContentLength: REQUEST_MAX_CONTENT_LENGTH, |
| 52 | + withCredentials: true, |
| 53 | + headers: { |
| 54 | + Authorization: `Bearer ${this.token}`, |
| 55 | + 'Content-Type': mimeType, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + return ax(axConfig); |
| 60 | + } |
| 61 | + |
| 62 | + static async preparePutUrl(attachmentService) { |
| 63 | + const service = attachmentService || ATTACHMENT_STORAGE_SERVICE_BASE_URL; |
| 64 | + const signedUrl = `${service}/objects/${uuid.v4()}`; |
| 65 | + |
| 66 | + this.emitter.logger.debug(`Attachment Storage Service signed url is ${signedUrl}`); |
| 67 | + return signedUrl; |
| 68 | + } |
| 69 | + |
| 70 | + static addRetryCountInterceptorToAxios(ax) { |
| 71 | + ax.interceptors.response.use(undefined, (err) => { // Retry count interceptor for axios |
| 72 | + const { config } = err; |
| 73 | + if (!config || !config.retry || !config.delay) { |
| 74 | + return Promise.reject(err); |
| 75 | + } |
| 76 | + config.currentRetryCount = config.currentRetryCount || 0; |
| 77 | + if (config.currentRetryCount >= config.retry) { |
| 78 | + return Promise.reject(err); |
| 79 | + } |
| 80 | + config.currentRetryCount += 1; |
| 81 | + return new Promise(resolve => setTimeout(() => resolve(ax(config)), config.delay)); |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
| 85 | +exports.AttachmentProcessor = AttachmentProcessor; |
0 commit comments