Wire up writeAttachment

This commit is contained in:
Daniel Gasienica 2018-03-14 20:44:45 -04:00
parent aa82a2a5fb
commit e0428355be
5 changed files with 27 additions and 3 deletions

View file

@ -5,6 +5,9 @@ const isString = require('lodash/isString');
const Path = require('path'); const Path = require('path');
// _writeAttachmentData :: AttachmentsPath ->
// ArrayBuffer ->
// IO (Promise Path)
exports.writeAttachmentData = (root) => { exports.writeAttachmentData = (root) => {
if (!isString(root)) { if (!isString(root)) {
throw new TypeError('`root` must be a path'); throw new TypeError('`root` must be a path');
@ -23,11 +26,13 @@ exports.writeAttachmentData = (root) => {
}; };
}; };
// _getAttachmentName :: Unit -> IO String
exports._getAttachmentName = () => { exports._getAttachmentName = () => {
const buffer = crypto.randomBytes(32); const buffer = crypto.randomBytes(32);
return buffer.toString('hex'); return buffer.toString('hex');
}; };
// _getAttachmentPath :: Unit -> IO Path
exports._getAttachmentPath = () => { exports._getAttachmentPath = () => {
const name = exports._getAttachmentName(); const name = exports._getAttachmentName();
const prefix = name.slice(0, 3); const prefix = name.slice(0, 3);

View file

@ -15,6 +15,7 @@
'use strict'; 'use strict';
const { Errors, Message } = window.Signal.Types; const { Errors, Message } = window.Signal.Types;
const { context: migrationsContext } = window.Signal.Migrations;
// Implicitly used in `indexeddb-backbonejs-adapter`: // Implicitly used in `indexeddb-backbonejs-adapter`:
// https://github.com/signalapp/Signal-Desktop/blob/4033a9f8137e62ed286170ed5d4941982b1d3a64/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js#L569 // https://github.com/signalapp/Signal-Desktop/blob/4033a9f8137e62ed286170ed5d4941982b1d3a64/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js#L569
@ -573,7 +574,8 @@
return event.confirm(); return event.confirm();
} }
const upgradedMessage = await Message.upgradeSchema(data.message); const upgradedMessage =
await Message.upgradeSchema(data.message, migrationsContext);
await ConversationController.getOrCreateAndWait( await ConversationController.getOrCreateAndWait(
messageDescriptor.id, messageDescriptor.id,
messageDescriptor.type messageDescriptor.type

View file

@ -10,6 +10,7 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
const { Attachment, Message } = window.Signal.Types; const { Attachment, Message } = window.Signal.Types;
const { context: migrationContext } = window.Signal.Migrations;
// TODO: Factor out private and group subclasses of Conversation // TODO: Factor out private and group subclasses of Conversation
@ -626,7 +627,7 @@
received_at: now, received_at: now,
expireTimer: this.get('expireTimer'), expireTimer: this.get('expireTimer'),
recipients: this.getRecipients(), recipients: this.getRecipients(),
}); }, migrationContext);
const message = this.messageCollection.add(messageWithSchema); const message = this.messageCollection.add(messageWithSchema);
if (this.isPrivate()) { if (this.isPrivate()) {
message.set({ destination: this.id }); message.set({ destination: this.id });

View file

@ -16,6 +16,7 @@ const {
const packageJson = require('./package.json'); const packageJson = require('./package.json');
const Attachments = require('./app/attachments');
const autoUpdate = require('./app/auto_update'); const autoUpdate = require('./app/auto_update');
const createTrayIcon = require('./app/tray_icon'); const createTrayIcon = require('./app/tray_icon');
const GlobalErrors = require('./js/modules/global_errors'); const GlobalErrors = require('./js/modules/global_errors');
@ -417,7 +418,7 @@ app.on('ready', () => {
let loggingSetupError; let loggingSetupError;
logging.initialize().catch((error) => { logging.initialize().catch((error) => {
loggingSetupError = error; loggingSetupError = error;
}).then(() => { }).then(async () => {
/* eslint-enable more/no-then */ /* eslint-enable more/no-then */
logger = logging.getLogger(); logger = logging.getLogger();
logger.info('app ready'); logger.info('app ready');
@ -431,6 +432,10 @@ app.on('ready', () => {
locale = loadLocale({ appLocale, logger }); locale = loadLocale({ appLocale, logger });
} }
console.log('Ensure attachments directory exists');
const userDataPath = app.getPath('userData');
await Attachments.ensureDirectory(userDataPath);
ready = true; ready = true;
autoUpdate.initialize(getMainWindow, locale.messages); autoUpdate.initialize(getMainWindow, locale.messages);

View file

@ -4,6 +4,11 @@
console.log('preload'); console.log('preload');
const electron = require('electron'); const electron = require('electron');
const Attachments = require('./app/attachments');
const { app } = electron.remote;
window.PROTO_ROOT = 'protos'; window.PROTO_ROOT = 'protos';
window.config = require('url').parse(window.location.toString(), true).query; window.config = require('url').parse(window.location.toString(), true).query;
window.wrapDeferred = function(deferred) { window.wrapDeferred = function(deferred) {
@ -110,6 +115,12 @@
window.Signal.Crypto = require('./js/modules/crypto'); window.Signal.Crypto = require('./js/modules/crypto');
window.Signal.Migrations = window.Signal.Migrations || {}; window.Signal.Migrations = window.Signal.Migrations || {};
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
const { writeAttachmentData } = require('./app/types/attachment/write_attachment_data');
// Injected context functions to keep `Message` agnostic from Electron:
window.Signal.Migrations.context = {
writeAttachmentData: writeAttachmentData(attachmentsPath),
};
window.Signal.Migrations.V17 = require('./js/modules/migrations/17'); window.Signal.Migrations.V17 = require('./js/modules/migrations/17');
window.Signal.Types = window.Signal.Types || {}; window.Signal.Types = window.Signal.Types || {};