Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy files to tmpdir #4604

Merged
merged 4 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Added
- highlight the first unread message upon opening a chat #4525
- copy files to internal tmp dir before opening attachements #4498
- enable notifications on mentions in muted chats #4538
- always show accounts with unread messages, even when they're normally scrolled out of view #4536

Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/components/message/messageFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export function onDownload(msg: Type.Message) {
}
}

export function openAttachmentInShell(msg: Type.Message) {
if (!msg.file) {
export async function openAttachmentInShell(msg: Type.Message) {
if (!msg.file || !msg.fileName) {
log.error('message has no file to open:', msg)
throw new Error('message has no file to open')
}
if (!runtime.openPath(msg.file)) {
const tmpFile = await runtime.copyFileToInternalTmpDir(msg.fileName, msg.file)
if (!runtime.openPath(tmpFile)) {
log.info(
"file couldn't be opened, try saving it in a different place and try to open it from there"
)
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export interface Runtime {
writeClipboardToTempFile(name?: string): Promise<string>
writeTempFileFromBase64(name: string, content: string): Promise<string>
writeTempFile(name: string, content: string): Promise<string>
copyFileToInternalTmpDir(
fileName: string,
sourcePath: string
): Promise<string>
removeTempFile(path: string): Promise<void>
getWebxdcDiskUsage(accountId: number): Promise<{
total_size: number
Expand Down
6 changes: 6 additions & 0 deletions packages/target-browser/runtime-browser/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ class BrowserRuntime implements Runtime {
).json()
).path
}
async copyFileToInternalTmpDir(
_fileName: string,
_sourcePath: string
): Promise<string> {
throw new Error('Method not implemented')
}
async removeTempFile(name: string): Promise<void> {
await fetch(`/backend-api/removeTempFile`, {
method: 'POST',
Expand Down
10 changes: 10 additions & 0 deletions packages/target-electron/runtime-electron/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ class ElectronRuntime implements Runtime {
writeTempFile(name: string, content: string): Promise<string> {
return ipcBackend.invoke('app.writeTempFile', name, content)
}
copyFileToInternalTmpDir(
fileName: string,
sourcePath: string
): Promise<string> {
return ipcBackend.invoke(
'app.copyFileToInternalTmpDir',
fileName,
sourcePath
)
}
removeTempFile(path: string): Promise<void> {
return ipcBackend.invoke('app.removeTempFile', path)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/target-electron/src/application-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ export const ALLOWED_ACCOUNT_FOLDERS = [
'dc.db-blobs',
'stickers',
]

// folder inside account dir to store files
// temporary when opened in another application
// cleared on app start
export const INTERNAL_TMP_DIR_NAME = 'tmp'
43 changes: 42 additions & 1 deletion packages/target-electron/src/cleanup_temp_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { app } from 'electron'
import { mkdir, readdir, rm, rmdir } from 'fs/promises'
import { join } from 'path'
import { getLogger } from '../../shared/logger.js'
import { getDraftTempDir } from './application-constants.js'
import {
getDraftTempDir,
getAccountsPath,
INTERNAL_TMP_DIR_NAME,
} from './application-constants.js'
import { readdirSync } from 'fs'

const log = getLogger('main/cleanup_temp_dir')

Expand Down Expand Up @@ -39,3 +44,39 @@ export async function cleanupDraftTempDir() {
log.error('Cleanup of old temp files failed: ', error)
}
}

/**
* clean tmp directory in all accounts
*/
export async function cleanupInternalTempDirs() {
try {
let deletedTmpDirs = 0
const tmpDirents = readdirSync(getAccountsPath(), {
withFileTypes: true,
})
.filter(dirent => dirent.isDirectory())
.flatMap(accountDir => {
return readdirSync(join(accountDir.parentPath, accountDir.name), {
withFileTypes: true,
}).filter(
tmpDir =>
tmpDir.isDirectory() && tmpDir.name === INTERNAL_TMP_DIR_NAME
)
})
if (tmpDirents.length > 0) {
deletedTmpDirs = (
await Promise.all(
tmpDirents.map(tmpDir =>
rm(join(tmpDir.parentPath, tmpDir.name), {
recursive: true,
force: true,
})
)
)
).length
}
log.info(`Deleted ${deletedTmpDirs} internal tmp directories`)
} catch (error) {
log.error('Cleanup of internal temp dirs failed: ', error)
}
}
7 changes: 5 additions & 2 deletions packages/target-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ import { updateTrayIcon, hideDeltaChat, showDeltaChat } from './tray.js'
import './notifications.js'
import { acceptThemeCLI } from './themes.js'
import { webxdcStartUpCleanup } from './deltachat/webxdc.js'
import { cleanupDraftTempDir } from './cleanup_temp_dir.js'
import {
cleanupDraftTempDir,
cleanupInternalTempDirs,
} from './cleanup_temp_dir.js'

app.ipcReady = false
app.isQuitting = false
Expand Down Expand Up @@ -198,7 +201,7 @@ async function onReady([_appReady, _loadedState, _appx, _webxdc_cleanup]: [
log.error('Cleanup of old logfiles failed: ', err)
)
cleanupDraftTempDir()

cleanupInternalTempDirs()
// NOTE: Make sure to use `powerMonitor` only when electron signals it is ready
initialisePowerMonitor()
}
Expand Down
22 changes: 22 additions & 0 deletions packages/target-electron/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getDraftTempDir,
getLogsPath,
htmlDistDir,
INTERNAL_TMP_DIR_NAME,
} from './application-constants.js'
import { LogHandler } from './log-handler.js'
import { ExtendedAppMainProcess } from './types.js'
Expand Down Expand Up @@ -256,6 +257,9 @@ export async function init(cwd: string, logHandler: LogHandler) {
ipcMain.handle('app.writeTempFile', (_ev, name, content) =>
writeTempFile(name, content)
)
ipcMain.handle('app.copyFileToInternalTmpDir', (_ev, name, pathToFile) => {
return copyFileToInternalTmpDir(name, pathToFile)
})
ipcMain.handle('app.removeTempFile', (_ev, path) => removeTempFile(path))

ipcMain.handle('electron.shell.openExternal', (_ev, url) =>
Expand Down Expand Up @@ -388,6 +392,24 @@ export async function writeTempFile(
return pathToFile
}

export async function copyFileToInternalTmpDir(
fileName: string,
sourcePath: string
): Promise<string> {
const sourceFileName = basename(sourcePath)
const sourceDir = dirname(sourcePath)
let destinationDir = join(sourceDir, '..', INTERNAL_TMP_DIR_NAME)
if (sourceFileName !== fileName) {
// this is the case, when we copy a file that has an identifier
// as name (given during the file deduplications process)
destinationDir = join(destinationDir, sourceFileName)
}
await mkdir(destinationDir, { recursive: true })
const targetPath = join(destinationDir, fileName)
await copyFile(sourcePath, targetPath)
return targetPath
}

async function removeTempFile(path: string) {
if (
path.indexOf(rawApp.getPath('temp')) === -1 ||
Expand Down
6 changes: 6 additions & 0 deletions packages/target-tauri/runtime-tauri/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ class TauriRuntime implements Runtime {
writeTempFile(_name: string, _content: string): Promise<string> {
throw new Error('Method not implemented.43')
}
copyFileToInternalTmpDir(
_fileName: string,
_sourcePath: string
): Promise<string> {
throw new Error('Method not implemented.44b')
}
removeTempFile(_path: string): Promise<void> {
throw new Error('Method not implemented.44')
}
Expand Down
Loading