override attachments name on upload

pull/1387/head
Audric Ackermann 5 years ago
parent 8abd6a0e21
commit 55fa65fc31
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -571,7 +571,6 @@
const isGroup = !!conversation && !conversation.isPrivate(); const isGroup = !!conversation && !conversation.isPrivate();
const attachments = this.get('attachments') || []; const attachments = this.get('attachments') || [];
const firstAttachment = attachments[0];
return { return {
text: this.createNonBreakingLastSeparator(this.get('body')), text: this.createNonBreakingLastSeparator(this.get('body')),
@ -987,6 +986,13 @@
attachments: attachmentsWithData, attachments: attachmentsWithData,
now: this.get('sent_at'), now: this.get('sent_at'),
}); });
const filenameOverridenAttachments = finalAttachments.map(attachment => ({
...attachment,
fileName: Signal.Types.Attachment.getSuggestedFilenameSending({
attachment,
timestamp: Date.now(),
}),
}));
const quoteWithData = await loadQuoteData(this.get('quote')); const quoteWithData = await loadQuoteData(this.get('quote'));
const previewWithData = await loadPreviewData(this.get('preview')); const previewWithData = await loadPreviewData(this.get('preview'));
@ -996,7 +1002,10 @@
const { AttachmentUtils } = libsession.Utils; const { AttachmentUtils } = libsession.Utils;
const [attachments, preview, quote] = await Promise.all([ const [attachments, preview, quote] = await Promise.all([
AttachmentUtils.uploadAttachments(finalAttachments, openGroup), AttachmentUtils.uploadAttachments(
filenameOverridenAttachments,
openGroup
),
AttachmentUtils.uploadLinkPreviews(previewWithData, openGroup), AttachmentUtils.uploadLinkPreviews(previewWithData, openGroup),
AttachmentUtils.uploadQuoteThumbnails(quoteWithData, openGroup), AttachmentUtils.uploadQuoteThumbnails(quoteWithData, openGroup),
]); ]);

@ -214,6 +214,7 @@ exports.deleteData = deleteOnDisk => {
exports.isVoiceMessage = AttachmentTS.isVoiceMessage; exports.isVoiceMessage = AttachmentTS.isVoiceMessage;
exports.save = AttachmentTS.save; exports.save = AttachmentTS.save;
exports.getFileExtension = AttachmentTS.getFileExtension; exports.getFileExtension = AttachmentTS.getFileExtension;
exports.getSuggestedFilenameSending = AttachmentTS.getSuggestedFilenameSending;
const THUMBNAIL_SIZE = 150; const THUMBNAIL_SIZE = 150;
const THUMBNAIL_CONTENT_TYPE = 'image/png'; const THUMBNAIL_CONTENT_TYPE = 'image/png';

@ -1340,6 +1340,7 @@ input {
position: absolute; position: absolute;
bottom: 15px; bottom: 15px;
right: 25px; right: 25px;
z-index: 2;
.session-icon-button { .session-icon-button {
display: flex; display: flex;

@ -181,7 +181,6 @@ export class Message extends React.PureComponent<Props, State> {
this.checkExpired(); this.checkExpired();
} }
public checkExpired() { public checkExpired() {
const now = Date.now(); const now = Date.now();
const { isExpired, expirationTimestamp, expirationLength } = this.props; const { isExpired, expirationTimestamp, expirationLength } = this.props;
@ -956,7 +955,6 @@ export class Message extends React.PureComponent<Props, State> {
} = this.props; } = this.props;
const { expired, expiring } = this.state; const { expired, expiring } = this.state;
if (expired) { if (expired) {
return null; return null;
} }
@ -988,7 +986,11 @@ export class Message extends React.PureComponent<Props, State> {
} }
return ( return (
<div id={id} className={classNames(divClasses)} onContextMenu={this.handleContextMenu}> <div
id={id}
className={classNames(divClasses)}
onContextMenu={this.handleContextMenu}
>
{this.renderAvatar()} {this.renderAvatar()}
<div <div
className={classNames( className={classNames(
@ -1006,7 +1008,10 @@ export class Message extends React.PureComponent<Props, State> {
// User clicked on message body // User clicked on message body
const target = event.target as HTMLDivElement; const target = event.target as HTMLDivElement;
if (!multiSelectMode && target.className === 'text-selectable' || window.contextMenuShown) { if (
(!multiSelectMode && target.className === 'text-selectable') ||
window.contextMenuShown
) {
return; return;
} }
@ -1035,7 +1040,10 @@ export class Message extends React.PureComponent<Props, State> {
// User clicked on message body // User clicked on message body
const target = event.target as HTMLDivElement; const target = event.target as HTMLDivElement;
if (target.className === 'text-selectable' || window.contextMenuShown) { if (
target.className === 'text-selectable' ||
window.contextMenuShown
) {
return; return;
} }
@ -1062,11 +1070,7 @@ export class Message extends React.PureComponent<Props, State> {
private handleContextMenu(e: any) { private handleContextMenu(e: any) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
const { const { isRss, multiSelectMode, isKickedFromGroup } = this.props;
isRss,
multiSelectMode,
isKickedFromGroup,
} = this.props;
const enableContextMenu = !isRss && !multiSelectMode && !isKickedFromGroup; const enableContextMenu = !isRss && !multiSelectMode && !isKickedFromGroup;
if (enableContextMenu) { if (enableContextMenu) {

@ -33,7 +33,7 @@ interface Props {
) => Promise<{ previousTopMessage: string }>; ) => Promise<{ previousTopMessage: string }>;
replyToMessage: (messageId: number) => Promise<void>; replyToMessage: (messageId: number) => Promise<void>;
onClickAttachment: (attachment: any, message: any) => void; onClickAttachment: (attachment: any, message: any) => void;
onDownloadAttachment: ({ attachment }: { attachment: any}) => void; onDownloadAttachment: ({ attachment }: { attachment: any }) => void;
} }
export class SessionConversationMessagesList extends React.Component< export class SessionConversationMessagesList extends React.Component<
@ -181,7 +181,7 @@ export class SessionConversationMessagesList extends React.Component<
this.props.onClickAttachment(attachment, messageProps); this.props.onClickAttachment(attachment, messageProps);
}; };
messageProps.onDownload = (attachment: AttachmentType) => { messageProps.onDownload = (attachment: AttachmentType) => {
this.props.onDownloadAttachment({attachment}); this.props.onDownloadAttachment({ attachment });
}; };
return <Message {...messageProps} />; return <Message {...messageProps} />;

@ -67,7 +67,6 @@ export class AttachmentUtils {
} }
server = openGroupServer; server = openGroupServer;
} }
const pointer: AttachmentPointer = { const pointer: AttachmentPointer = {
contentType: attachment.contentType contentType: attachment.contentType
? (attachment.contentType as string) ? (attachment.contentType as string)

@ -48,7 +48,6 @@ describe('Attachment', () => {
contentType: MIME.VIDEO_QUICKTIME, contentType: MIME.VIDEO_QUICKTIME,
url: 'funny-cat.mov', url: 'funny-cat.mov',
fileName: 'funny-cat.mov', fileName: 'funny-cat.mov',
}; };
const timestamp = moment('2000-01-01').toDate(); const timestamp = moment('2000-01-01').toDate();
const actual = Attachment.getSuggestedFilename({ const actual = Attachment.getSuggestedFilename({
@ -64,7 +63,8 @@ describe('Attachment', () => {
const attachment: Attachment.AttachmentType = { const attachment: Attachment.AttachmentType = {
fileName: 'funny-cat.mov', fileName: 'funny-cat.mov',
url: 'funny-cat.mov', url: 'funny-cat.mov',
contentType: MIME.VIDEO_QUICKTIME, }; contentType: MIME.VIDEO_QUICKTIME,
};
const timestamp = new Date(new Date(0).getTimezoneOffset() * 60 * 1000); const timestamp = new Date(new Date(0).getTimezoneOffset() * 60 * 1000);
const actual = Attachment.getSuggestedFilename({ const actual = Attachment.getSuggestedFilename({
attachment, attachment,

@ -369,6 +369,24 @@ export const getSuggestedFilename = ({
return `${prefix}${suffix}${indexSuffix}${extension}`; return `${prefix}${suffix}${indexSuffix}${extension}`;
}; };
// Used for overriden the sent filename of an attachment, but keeping the file extension the same
export const getSuggestedFilenameSending = ({
attachment,
timestamp,
}: {
attachment: AttachmentType;
timestamp?: number | Date;
}): string => {
const prefix = 'session-attachment';
const suffix = timestamp
? moment(timestamp).format('-YYYY-MM-DD-HHmmss')
: '';
const fileType = getFileExtension(attachment);
const extension = fileType ? `.${fileType}` : '';
return `${prefix}${suffix}${extension}`;
};
export const getFileExtension = ( export const getFileExtension = (
attachment: AttachmentType attachment: AttachmentType
): string | undefined => { ): string | undefined => {

Loading…
Cancel
Save