-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
|
||
import type { FileAttachmentHandler } from '../types'; | ||
import { fastMove } from '../../utils'; | ||
|
||
import { placeAttachment } from './placeAttachment'; | ||
|
||
export const moveHandler: FileAttachmentHandler = async (context) => { | ||
const destination = placeAttachment(context); | ||
await fs.mkdir(path.dirname(destination), { recursive: true }); | ||
await fs.rename(context.sourcePath, destination); | ||
await fastMove(context.sourcePath, destination); | ||
return destination; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
|
||
import { fastMove } from './fastMove'; | ||
|
||
describe('fastMove', () => { | ||
let rootDirectory: string; | ||
let source: string; | ||
let destination: string; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(fs, 'copyFile'); | ||
jest.spyOn(fs, 'rm'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
rootDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'fastmove-')); | ||
source = path.join(rootDirectory, 'source.txt'); | ||
destination = path.join(rootDirectory, 'destination.txt'); | ||
await fs.writeFile(source, 'Test content'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await fs.rm(rootDirectory, { recursive: true }); | ||
}); | ||
|
||
it('should move the file to the destination', async () => { | ||
await fastMove(source, destination); | ||
|
||
const movedFile = await fs.readFile(destination, 'utf8'); | ||
expect(movedFile).toBe('Test content'); | ||
await expect(fs.access(source)).rejects.toThrow(); | ||
expect(fs.copyFile).not.toHaveBeenCalled(); | ||
expect(fs.rm).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle cross-device file movement', async () => { | ||
jest | ||
.spyOn(fs, 'rename') | ||
.mockRejectedValueOnce({ code: 'EXDEV', message: 'cross-device link not permitted' }); | ||
|
||
await fastMove(source, destination); | ||
|
||
const movedFile = await fs.readFile(destination, 'utf8'); | ||
expect(movedFile).toBe('Test content'); | ||
await expect(fs.access(source)).rejects.toThrow(); | ||
expect(fs.copyFile).toHaveBeenCalled(); | ||
expect(fs.rm).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
|
||
export async function fastMove(source: string, destination: string) { | ||
await fs.mkdir(path.dirname(destination), { recursive: true }); | ||
|
||
try { | ||
await fs.rename(source, destination); | ||
} catch { | ||
await fs.copyFile(source, destination); | ||
await fs.rm(source, { force: true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getFullExtension } from './getFullExtension'; | ||
|
||
describe('getFullExtension', () => { | ||
it('should return the full extension for a file with multiple dots', () => { | ||
const filePath = 'example.viewhierarchy.zip'; | ||
const extension = getFullExtension(filePath); | ||
expect(extension).toBe('.viewhierarchy.zip'); | ||
}); | ||
|
||
it('should return the extension for a file with a single dot', () => { | ||
const filePath = 'example.txt'; | ||
const extension = getFullExtension(filePath); | ||
expect(extension).toBe('.txt'); | ||
}); | ||
|
||
it('should return the extension for a file starting with a dot', () => { | ||
const filePath = '.gitignore'; | ||
const extension = getFullExtension(filePath); | ||
expect(extension).toBe('.gitignore'); | ||
}); | ||
|
||
it('should return an empty string for a file without an extension', () => { | ||
const filePath = 'example'; | ||
const extension = getFullExtension(filePath); | ||
expect(extension).toBe(''); | ||
}); | ||
|
||
it('should return an empty string for empty or dot paths', () => { | ||
expect(getFullExtension('')).toBe(''); | ||
expect(getFullExtension('.')).toBe(''); | ||
expect(getFullExtension('..')).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import path from 'node:path'; | ||
|
||
export function getFullExtension(filePath: string) { | ||
if (!filePath || filePath === '.' || filePath === '..') return ''; | ||
|
||
const fileName = path.basename(filePath); | ||
const lastDotIndex = fileName.indexOf('.'); | ||
|
||
return lastDotIndex === -1 ? '' : fileName.slice(lastDotIndex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters