Skip to content

Commit 1c76283

Browse files
committed
feat(Admin > Resize): Move MP4s to videos folder
1 parent 44daf73 commit 1c76283

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/lib/__tests__/rename-moveRaw.vitest.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,32 @@ describe('moveRaws function', () => {
2222
beforeEach(() => {
2323
vi.resetAllMocks()
2424
originalPath = '/'
25-
filesOnDisk = ['image.heic', 'photo.heif', 'clip.raw', 'movie.mov', 'document.txt', 'photo.jpg']
25+
filesOnDisk = [
26+
'image.heic', 'photo.heif', 'clip.raw', 'movie.mov',
27+
'document.txt', 'photo.jpg', 'video.mp4', 'video.orig.mp4',
28+
]
2629
errors = []
2730
formatErrorMessage = vi.fn((err, msg) => `${msg}: ${err.message}`)
2831
})
2932

3033

31-
test("should create 'raws' folder and move all configured raw files", async () => {
34+
test("should create 'raws' and 'videos' folder and move all configured raw and video files", async () => {
3235
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
3336
vi.mocked(fs.rename).mockResolvedValue(undefined)
3437

3538
await moveRaws({ originalPath, filesOnDisk, errors, formatErrorMessage })
3639

3740
expect(fs.mkdir).toHaveBeenCalledWith(path.join(originalPath, 'raws'), { recursive: true })
38-
expect(fs.rename).toHaveBeenCalledTimes(4)
41+
expect(fs.mkdir).toHaveBeenCalledWith(path.join(originalPath, 'videos'), { recursive: true })
42+
// 4 raw + 1 mp4 = 5 moves (movie.mov is raw, video.orig.mp4 should NOT be moved)
43+
expect(fs.rename).toHaveBeenCalledTimes(5)
3944
expect(fs.rename).toHaveBeenCalledWith(path.join(originalPath, 'image.heic'), path.join(originalPath, 'raws/image.heic'))
4045
expect(fs.rename).toHaveBeenCalledWith(path.join(originalPath, 'photo.heif'), path.join(originalPath, 'raws/photo.heif'))
4146
expect(fs.rename).toHaveBeenCalledWith(path.join(originalPath, 'clip.raw'), path.join(originalPath, 'raws/clip.raw'))
4247
expect(fs.rename).toHaveBeenCalledWith(path.join(originalPath, 'movie.mov'), path.join(originalPath, 'raws/movie.mov'))
48+
expect(fs.rename).toHaveBeenCalledWith(path.join(originalPath, 'video.mp4'), path.join(originalPath, 'videos/video.mp4'))
49+
// .orig.mp4 should NOT be moved
50+
expect(fs.rename).not.toHaveBeenCalledWith(path.join(originalPath, 'video.orig.mp4'), expect.any(String))
4351
expect(errors).toHaveLength(0)
4452
})
4553

@@ -49,10 +57,14 @@ describe('moveRaws function', () => {
4957

5058
await moveRaws({ originalPath, filesOnDisk, errors, formatErrorMessage })
5159

52-
// Only raw files should trigger errors
53-
expect(errors).toHaveLength(4)
54-
expect(formatErrorMessage).toHaveBeenCalledTimes(4)
60+
// 4 raw files + 1 video file should trigger errors
61+
expect(errors).toHaveLength(5)
62+
expect(formatErrorMessage).toHaveBeenCalledTimes(5)
5563
expect(errors[0]).toContain('Error moving raw file: image.heic')
64+
expect(errors[1]).toContain('Error moving raw file: photo.heif')
65+
expect(errors[2]).toContain('Error moving raw file: clip.raw')
66+
expect(errors[3]).toContain('Error moving raw file: movie.mov')
67+
expect(errors[4]).toContain('Error moving video file: video.mp4')
5668
})
5769

5870
test('should not move files not in config', async () => {

src/lib/rename.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ async function moveRaws(
124124
{ originalPath: string, filesOnDisk: string[], errors: string[], formatErrorMessage: ErrorFormatter },
125125
) {
126126
const rawsPath = path.join(path.dirname(originalPath), 'raws')
127+
const videosPath = path.join(path.dirname(originalPath), 'videos')
127128
await fs.mkdir(rawsPath, { recursive: true })
129+
await fs.mkdir(videosPath, { recursive: true })
128130

129131
// Collect all raw extensions from config (lowercase, with dot)
130132
const rawExtensions = new Set(
@@ -134,6 +136,11 @@ async function moveRaws(
134136
...(config.rawFileTypes?.video ?? []),
135137
].map(ext => `.${ext.toLowerCase()}`),
136138
)
139+
const videoExtensions = new Set(
140+
[
141+
...['mp4'],
142+
].map(ext => `.${ext.toLowerCase()}`),
143+
)
137144

138145
for (const file of filesOnDisk) {
139146
const ext = path.extname(file).toLowerCase()
@@ -148,6 +155,21 @@ async function moveRaws(
148155
errors.push(formatErrorMessage(err, `Error moving raw file: ${file}`))
149156
}
150157
}
158+
// Skip .orig.mp4 files for video
159+
if (
160+
videoExtensions.has(ext) &&
161+
!file.toLowerCase().endsWith('.orig.mp4')
162+
) {
163+
const sourceFile = path.join(originalPath, file)
164+
const destinationFile = path.join(videosPath, file)
165+
166+
try {
167+
await fs.rename(sourceFile, destinationFile) // Move file
168+
console.log(`Moved: ${file} → videos`)
169+
} catch (err) {
170+
errors.push(formatErrorMessage(err, `Error moving video file: ${file}`))
171+
}
172+
}
151173
}
152174
}
153175

0 commit comments

Comments
 (0)