diff --git a/package-lock.json b/package-lock.json index fdac779f3..eed23b554 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,9 +11,11 @@ "dependencies": { "axios": "^1.7.9", "formdata-node": "^6.0.3", + "mime-types": "^2.1.35", "tslib": "^2.8.1" }, "devDependencies": { + "@types/mime-types": "^2.1.4", "@types/node": "^18.19.69", "@types/sinon": "^17.0.3", "@typescript-eslint/eslint-plugin": "^8.19.0", @@ -1048,6 +1050,13 @@ "@types/unist": "*" } }, + "node_modules/@types/mime-types": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.4.tgz", + "integrity": "sha512-lfU4b34HOri+kAY5UheuFMWPDOI+OPceBSHZKp69gEyTL/mmJ4cnU6Y/rlme3UL3GyOn6Y42hyIEw0/q8sWx5w==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/ms": { "version": "0.7.34", "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.34.tgz", diff --git a/package.json b/package.json index b9d82215a..fe3e11941 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "code:formatting": "npm run replace:all && npm run prettier && npm run lint:fix" }, "devDependencies": { + "@types/mime-types": "^2.1.4", "@types/node": "^18.19.69", "@types/sinon": "^17.0.3", "@typescript-eslint/eslint-plugin": "^8.19.0", @@ -73,6 +74,7 @@ "dependencies": { "axios": "^1.7.9", "formdata-node": "^6.0.3", + "mime-types": "^2.1.35", "tslib": "^2.8.1" } } diff --git a/src/serviceDesk/parameters/attachTemporaryFile.ts b/src/serviceDesk/parameters/attachTemporaryFile.ts index f1ea8ab2b..ed93037e0 100644 --- a/src/serviceDesk/parameters/attachTemporaryFile.ts +++ b/src/serviceDesk/parameters/attachTemporaryFile.ts @@ -1,6 +1,7 @@ export interface Attachment { filename: string; file: Buffer | ReadableStream | string | Blob | File; + mimeType?: string; } export interface AttachTemporaryFile { diff --git a/src/serviceDesk/serviceDesk.ts b/src/serviceDesk/serviceDesk.ts index ec3d824f1..99123a87e 100644 --- a/src/serviceDesk/serviceDesk.ts +++ b/src/serviceDesk/serviceDesk.ts @@ -1,4 +1,5 @@ import { FormData } from 'formdata-node'; +import * as mime from 'mime-types'; import * as Models from './models'; import * as Parameters from './parameters'; import { Callback } from '../callback'; @@ -116,7 +117,14 @@ export class ServiceDesk { const formData = new FormData(); const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment]; - attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename)); + attachments.forEach(attachment => { + const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined); + const file = Buffer.isBuffer(attachment.file) + ? new File([attachment.file], attachment.filename, { type: mimeType }) + : attachment.file; + + formData.append('file', file, attachment.filename); + }); const config: RequestConfig = { url: `/rest/servicedeskapi/servicedesk/${parameters.serviceDeskId}/attachTemporaryFile`, diff --git a/src/version2/issueAttachments.ts b/src/version2/issueAttachments.ts index 33bad5b82..29dc8fb56 100644 --- a/src/version2/issueAttachments.ts +++ b/src/version2/issueAttachments.ts @@ -1,4 +1,5 @@ import { FormData } from 'formdata-node'; +import * as mime from 'mime-types'; import * as Models from './models'; import * as Parameters from './parameters'; import { Callback } from '../callback'; @@ -425,7 +426,14 @@ export class IssueAttachments { const formData = new FormData(); const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment]; - attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename)); + attachments.forEach(attachment => { + const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined); + const file = Buffer.isBuffer(attachment.file) + ? new File([attachment.file], attachment.filename, { type: mimeType }) + : attachment.file; + + formData.append('file', file, attachment.filename); + }); const config: RequestConfig = { url: `/rest/api/2/issue/${parameters.issueIdOrKey}/attachments`, diff --git a/src/version2/parameters/addAttachment.ts b/src/version2/parameters/addAttachment.ts index aff6535be..252176395 100644 --- a/src/version2/parameters/addAttachment.ts +++ b/src/version2/parameters/addAttachment.ts @@ -1,6 +1,7 @@ export interface Attachment { filename: string; file: Buffer | ReadableStream | string | Blob | File; + mimeType?: string; } export interface AddAttachment { diff --git a/src/version3/issueAttachments.ts b/src/version3/issueAttachments.ts index b134f7a4f..3eaa7b45d 100644 --- a/src/version3/issueAttachments.ts +++ b/src/version3/issueAttachments.ts @@ -1,4 +1,5 @@ import { FormData } from 'formdata-node'; +import * as mime from 'mime-types'; import * as Models from './models'; import * as Parameters from './parameters'; import { Callback } from '../callback'; @@ -425,7 +426,14 @@ export class IssueAttachments { const formData = new FormData(); const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment]; - attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename)); + attachments.forEach(attachment => { + const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined); + const file = Buffer.isBuffer(attachment.file) + ? new File([attachment.file], attachment.filename, { type: mimeType }) + : attachment.file; + + formData.append('file', file, attachment.filename); + }); const config: RequestConfig = { url: `/rest/api/3/issue/${parameters.issueIdOrKey}/attachments`, diff --git a/src/version3/parameters/addAttachment.ts b/src/version3/parameters/addAttachment.ts index aff6535be..252176395 100644 --- a/src/version3/parameters/addAttachment.ts +++ b/src/version3/parameters/addAttachment.ts @@ -1,6 +1,7 @@ export interface Attachment { filename: string; file: Buffer | ReadableStream | string | Blob | File; + mimeType?: string; } export interface AddAttachment {