|
|
@ -16,6 +16,17 @@ import { PresignedUrl } from '~/models'; |
|
|
|
import { utf8ify } from '~/helpers/stringHelpers'; |
|
|
|
import { utf8ify } from '~/helpers/stringHelpers'; |
|
|
|
import { NcError } from '~/helpers/catchError'; |
|
|
|
import { NcError } from '~/helpers/catchError'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface AttachmentObject { |
|
|
|
|
|
|
|
url?: string; |
|
|
|
|
|
|
|
path?: string; |
|
|
|
|
|
|
|
title: string; |
|
|
|
|
|
|
|
mimetype: string; |
|
|
|
|
|
|
|
size: number; |
|
|
|
|
|
|
|
icon?: string; |
|
|
|
|
|
|
|
signedPath?: string; |
|
|
|
|
|
|
|
signedUrl?: string; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Injectable() |
|
|
|
@Injectable() |
|
|
|
export class AttachmentsService { |
|
|
|
export class AttachmentsService { |
|
|
|
protected logger = new Logger(AttachmentsService.name); |
|
|
|
protected logger = new Logger(AttachmentsService.name); |
|
|
@ -54,45 +65,19 @@ export class AttachmentsService { |
|
|
|
file, |
|
|
|
file, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const attachment: { |
|
|
|
const attachment: AttachmentObject = { |
|
|
|
url?: string; |
|
|
|
...(url |
|
|
|
path?: string; |
|
|
|
? { url } |
|
|
|
title: string; |
|
|
|
: { |
|
|
|
mimetype: string; |
|
|
|
path: path.join('download', filePath.join('/'), fileName), |
|
|
|
size: number; |
|
|
|
}), |
|
|
|
icon?: string; |
|
|
|
|
|
|
|
signedPath?: string; |
|
|
|
|
|
|
|
signedUrl?: string; |
|
|
|
|
|
|
|
} = { |
|
|
|
|
|
|
|
...(url ? { url } : {}), |
|
|
|
|
|
|
|
title: originalName, |
|
|
|
title: originalName, |
|
|
|
mimetype: file.mimetype, |
|
|
|
mimetype: file.mimetype, |
|
|
|
size: file.size, |
|
|
|
size: file.size, |
|
|
|
icon: mimeIcons[path.extname(originalName).slice(1)] || undefined, |
|
|
|
icon: mimeIcons[path.extname(originalName).slice(1)] || undefined, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// if `url` is null, then it is local attachment
|
|
|
|
await this.signAttachment({ attachment }); |
|
|
|
if (!url) { |
|
|
|
|
|
|
|
// then store the attachment path only
|
|
|
|
|
|
|
|
// url will be constructed in `useAttachmentCell`
|
|
|
|
|
|
|
|
attachment.path = path.join( |
|
|
|
|
|
|
|
'download', |
|
|
|
|
|
|
|
filePath.join('/'), |
|
|
|
|
|
|
|
fileName, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attachment.signedPath = await PresignedUrl.getSignedUrl({ |
|
|
|
|
|
|
|
pathOrUrl: attachment.path.replace(/^download\//, ''), |
|
|
|
|
|
|
|
preview: true, |
|
|
|
|
|
|
|
mimetype: attachment.mimetype, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
attachment.signedUrl = await PresignedUrl.getSignedUrl({ |
|
|
|
|
|
|
|
pathOrUrl: attachment.url, |
|
|
|
|
|
|
|
preview: true, |
|
|
|
|
|
|
|
mimetype: attachment.mimetype, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attachments.push(attachment); |
|
|
|
attachments.push(attachment); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
@ -159,25 +144,30 @@ export class AttachmentsService { |
|
|
|
slash(path.join(destPath, fileName)), |
|
|
|
slash(path.join(destPath, fileName)), |
|
|
|
finalUrl, |
|
|
|
finalUrl, |
|
|
|
); |
|
|
|
); |
|
|
|
// if `attachmentUrl` is null, then it is local attachment
|
|
|
|
|
|
|
|
// then store the attachment path only
|
|
|
|
let mimeType = response.headers['content-type']?.split(';')[0]; |
|
|
|
// url will be constructed in `useAttachmentCell`
|
|
|
|
|
|
|
|
const attachmentPath = !attachmentUrl |
|
|
|
|
|
|
|
? `download/${filePath.join('/')}/${fileName}` |
|
|
|
|
|
|
|
: undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const mimeType = response.headers['content-type']?.split(';')[0]; |
|
|
|
|
|
|
|
const size = response.headers['content-length']; |
|
|
|
const size = response.headers['content-length']; |
|
|
|
|
|
|
|
|
|
|
|
attachments.push({ |
|
|
|
if (!mimeType) { |
|
|
|
...(attachmentUrl ? { url: finalUrl } : {}), |
|
|
|
mimeType = mimetypes[path.extname(fileNameWithExt).slice(1)]; |
|
|
|
...(attachmentPath ? { path: attachmentPath } : {}), |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const attachment: AttachmentObject = { |
|
|
|
|
|
|
|
...(attachmentUrl |
|
|
|
|
|
|
|
? { url: attachmentUrl } |
|
|
|
|
|
|
|
: { |
|
|
|
|
|
|
|
path: path.join('download', filePath.join('/'), fileName), |
|
|
|
|
|
|
|
}), |
|
|
|
title: fileNameWithExt, |
|
|
|
title: fileNameWithExt, |
|
|
|
mimetype: mimeType || urlMeta.mimetype, |
|
|
|
mimetype: mimeType || urlMeta.mimetype, |
|
|
|
size: size ? parseInt(size) : urlMeta.size, |
|
|
|
size: size ? parseInt(size) : urlMeta.size, |
|
|
|
icon: |
|
|
|
icon: |
|
|
|
mimeIcons[path.extname(fileNameWithExt).slice(1)] || undefined, |
|
|
|
mimeIcons[path.extname(fileNameWithExt).slice(1)] || undefined, |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.signAttachment({ attachment }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attachments.push(attachment); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
errors.push(e); |
|
|
|
errors.push(e); |
|
|
|
} |
|
|
|
} |
|
|
@ -237,26 +227,44 @@ export class AttachmentsService { |
|
|
|
NcError.genericNotFound('Attachment', urlOrPath); |
|
|
|
NcError.genericNotFound('Attachment', urlOrPath); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fileObject?.path) { |
|
|
|
await this.signAttachment({ |
|
|
|
const signedPath = await PresignedUrl.getSignedUrl({ |
|
|
|
attachment: fileObject, |
|
|
|
pathOrUrl: fileObject.path.replace(/^download\//, ''), |
|
|
|
preview: false, |
|
|
|
preview: false, |
|
|
|
filename: fileObject.title, |
|
|
|
filename: fileObject.title, |
|
|
|
expireSeconds: 5 * 60, |
|
|
|
mimetype: fileObject.mimetype, |
|
|
|
}); |
|
|
|
expireSeconds: 5 * 60, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { path: signedPath }; |
|
|
|
return { |
|
|
|
} else if (fileObject?.url) { |
|
|
|
...(fileObject?.path |
|
|
|
const signedUrl = await PresignedUrl.getSignedUrl({ |
|
|
|
? { path: fileObject.signedPath } |
|
|
|
pathOrUrl: fileObject.url, |
|
|
|
: { |
|
|
|
preview: false, |
|
|
|
url: fileObject.signedUrl, |
|
|
|
filename: fileObject.title, |
|
|
|
}), |
|
|
|
mimetype: fileObject.mimetype, |
|
|
|
}; |
|
|
|
expireSeconds: 5 * 60, |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { url: signedUrl }; |
|
|
|
async signAttachment(param: { |
|
|
|
|
|
|
|
attachment: AttachmentObject; |
|
|
|
|
|
|
|
preview?: boolean; |
|
|
|
|
|
|
|
filename?: string; |
|
|
|
|
|
|
|
expireSeconds?: number; |
|
|
|
|
|
|
|
}) { |
|
|
|
|
|
|
|
const { attachment, preview = true, ...extra } = param; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (attachment?.path) { |
|
|
|
|
|
|
|
attachment.signedPath = await PresignedUrl.getSignedUrl({ |
|
|
|
|
|
|
|
pathOrUrl: attachment.path.replace(/^download\//, ''), |
|
|
|
|
|
|
|
preview, |
|
|
|
|
|
|
|
mimetype: attachment.mimetype, |
|
|
|
|
|
|
|
...(extra ? { ...extra } : {}), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} else if (attachment?.url) { |
|
|
|
|
|
|
|
attachment.signedUrl = await PresignedUrl.getSignedUrl({ |
|
|
|
|
|
|
|
pathOrUrl: attachment.url, |
|
|
|
|
|
|
|
preview, |
|
|
|
|
|
|
|
mimetype: attachment.mimetype, |
|
|
|
|
|
|
|
...(extra ? { ...extra } : {}), |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|