On attachment save include date, include album index

pull/272/head
Scott Nonnenberg 6 years ago
parent 0b0dfbce9d
commit bf904ddd12

@ -1294,8 +1294,9 @@
Signal.Types.Attachment.save({ Signal.Types.Attachment.save({
attachment: options.attachment, attachment: options.attachment,
document, document,
index: options.index + 1,
getAbsolutePath: getAbsoluteAttachmentPath, getAbsolutePath: getAbsoluteAttachmentPath,
timestamp: options.message.received_at, timestamp: options.message.get('sent_at'),
}); });
}; };

@ -24,7 +24,7 @@ interface Props {
i18n: Localizer; i18n: Localizer;
media: Array<MediaItemType>; media: Array<MediaItemType>;
onSave?: ( onSave?: (
{ attachment, message }: { attachment: AttachmentType; message: Message } options: { attachment: AttachmentType; message: Message; index: number }
) => void; ) => void;
selectedIndex: number; selectedIndex: number;
} }
@ -98,8 +98,8 @@ export class LightboxGallery extends React.Component<Props, State> {
const { selectedIndex } = this.state; const { selectedIndex } = this.state;
const mediaItem = media[selectedIndex]; const mediaItem = media[selectedIndex];
const { attachment, message } = mediaItem; const { attachment, message, index } = mediaItem;
onSave({ attachment, message }); onSave({ attachment, message, index });
}; };
} }

@ -53,6 +53,22 @@ describe('Attachment', () => {
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
}); });
context('for attachment with index', () => {
it('should generate a filename based on timestamp', () => {
const attachment: Attachment.Attachment = {
data: stringToArrayBuffer('foo'),
contentType: MIME.VIDEO_QUICKTIME,
};
const timestamp = new Date(new Date(0).getTimezoneOffset() * 60 * 1000);
const actual = Attachment.getSuggestedFilename({
attachment,
timestamp,
index: 3,
});
const expected = 'signal-attachment-1970-01-01-000000_003.mov';
assert.strictEqual(actual, expected);
});
});
}); });
describe('isVisualMedia', () => { describe('isVisualMedia', () => {

@ -1,5 +1,6 @@
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import moment from 'moment'; import moment from 'moment';
import { padStart } from 'lodash';
import * as MIME from './MIME'; import * as MIME from './MIME';
import { arrayBufferToObjectURL } from '../util/arrayBufferToObjectURL'; import { arrayBufferToObjectURL } from '../util/arrayBufferToObjectURL';
@ -82,11 +83,13 @@ export const isVoiceMessage = (attachment: Attachment): boolean => {
export const save = ({ export const save = ({
attachment, attachment,
document, document,
index,
getAbsolutePath, getAbsolutePath,
timestamp, timestamp,
}: { }: {
attachment: Attachment; attachment: Attachment;
document: Document; document: Document;
index: number;
getAbsolutePath: (relativePath: string) => string; getAbsolutePath: (relativePath: string) => string;
timestamp?: number; timestamp?: number;
}): void => { }): void => {
@ -97,7 +100,7 @@ export const save = ({
data: attachment.data, data: attachment.data,
type: MIME.APPLICATION_OCTET_STREAM, type: MIME.APPLICATION_OCTET_STREAM,
}); });
const filename = getSuggestedFilename({ attachment, timestamp }); const filename = getSuggestedFilename({ attachment, timestamp, index });
saveURLAsFile({ url, filename, document }); saveURLAsFile({ url, filename, document });
if (isObjectURLRequired) { if (isObjectURLRequired) {
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
@ -107,9 +110,11 @@ export const save = ({
export const getSuggestedFilename = ({ export const getSuggestedFilename = ({
attachment, attachment,
timestamp, timestamp,
index,
}: { }: {
attachment: Attachment; attachment: Attachment;
timestamp?: number | Date; timestamp?: number | Date;
index?: number;
}): string => { }): string => {
if (attachment.fileName) { if (attachment.fileName) {
return attachment.fileName; return attachment.fileName;
@ -121,8 +126,9 @@ export const getSuggestedFilename = ({
: ''; : '';
const fileType = getFileExtension(attachment); const fileType = getFileExtension(attachment);
const extension = fileType ? `.${fileType}` : ''; const extension = fileType ? `.${fileType}` : '';
const indexSuffix = index ? `_${padStart(index.toString(), 3, '0')}` : '';
return `${prefix}${suffix}${extension}`; return `${prefix}${suffix}${indexSuffix}${extension}`;
}; };
export const getFileExtension = (attachment: Attachment): string | null => { export const getFileExtension = (attachment: Attachment): string | null => {

Loading…
Cancel
Save