-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathfile-path.service.ts
70 lines (58 loc) · 2.04 KB
/
file-path.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Injectable } from '@angular/core';
import * as path from 'path';
import { SourceFolderService } from '../statistics/source-folder.service';
import type { ImageElement } from '../../../../interfaces/final-object.interface';
type FolderType = 'thumbnails' | 'filmstrips' | 'clips';
@Injectable()
export class FilePathService {
replaceMap: any = {
' ': '%20',
'(': '%28',
')': '%29',
};
constructor(
public sourceFolderService: SourceFolderService,
) { }
/**
* Build the browser-friendly path based on the input (only `/` and `%20`), prepend with `file://`
* @param folderPath - path to where `vha-folder` is stored
* @param hubName - name of hub (to pick the correct `vha-folder` name)
* @param subfolder - whether `thumbnails`, `filmstrips`, or `clips`
* @param hash - file hash
* @param video - boolean -- if true then extension is `.mp4`
*/
createFilePath(folderPath: string, hubName: string, subfolder: FolderType, hash: string, video?: boolean): string {
return 'file://' + path.normalize(path.join(
folderPath,
'vha-' + hubName,
subfolder,
hash + (video ? '.mp4' : '.jxl')
)).replace(/\\/g, '/')
.replace(/[ \(\)]/g, (match) => { return this.replaceMap[match]; });
// ^^^^^ replace the ` ` (space) as well as parentheses `(` and `)` with URL encoding from the `replaceMap`
}
/**
* return file name without extension
* e.g. `video.mp4` => `video`
*/
getFileNameWithoutExtension(fileName: string): string {
return fileName.slice().substr(0, fileName.lastIndexOf('.'));
}
/**
* return extension without file name
* e.g. `video.mp4` => `.mp4`
*/
getFileNameExtension(fileName: string): string {
return fileName.slice().split('.').pop();
}
/**
* Return full filesystem path to video file
*/
getPathFromImageElement(item: ImageElement): string {
return path.join(
this.sourceFolderService.selectedSourceFolder[item.inputSource].path,
item.partialPath,
item.fileName
);
}
}