File sizes

This commit is contained in:
Manav Rathi 2024-04-25 15:47:34 +05:30
parent ac9a272c70
commit ca0afc2f3a
No known key found for this signature in database
6 changed files with 37 additions and 11 deletions

View file

@ -27,3 +27,5 @@ export const fsIsDir = async (dirPath: string) => {
const stat = await fs.stat(dirPath);
return stat.isDirectory();
};
export const fsSize = (path: string) => fs.stat(path).then((s) => s.size);

View file

@ -29,6 +29,7 @@ import {
fsRename,
fsRm,
fsRmdir,
fsSize,
fsWriteFile,
} from "./fs";
import { logToDisk } from "./log";
@ -139,6 +140,8 @@ export const attachIPCHandlers = () => {
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
ipcMain.handle("fsSize", (_, path: string) => fsSize(path));
// - Conversion
ipcMain.handle("convertToJPEG", (_, imageData: Uint8Array) =>

View file

@ -122,6 +122,9 @@ const fsWriteFile = (path: string, contents: string): Promise<void> =>
const fsIsDir = (dirPath: string): Promise<boolean> =>
ipcRenderer.invoke("fsIsDir", dirPath);
const fsSize = (path: string): Promise<number> =>
ipcRenderer.invoke("fsSize", path);
// - Conversion
const convertToJPEG = (imageData: Uint8Array): Promise<Uint8Array> =>
@ -331,6 +334,7 @@ contextBridge.exposeInMainWorld("electron", {
readTextFile: fsReadTextFile,
writeFile: fsWriteFile,
isDir: fsIsDir,
size: fsSize,
},
// - Conversion

View file

@ -50,6 +50,7 @@ import {
} from "./takeout";
import UploadService, {
assetName,
fopSize,
getAssetName,
getFileName,
uploader,
@ -396,7 +397,7 @@ class UploadManager {
if (mediaFiles.length) {
/* TODO(MR): ElectronFile changes */
const clusteredMediaFiles = clusterLivePhotos(
const clusteredMediaFiles = await clusterLivePhotos(
mediaFiles as ClusterableFile[],
);
@ -775,7 +776,7 @@ type ClusteredFile = {
* Go through the given files, combining any sibling image + video assets into a
* single live photo when appropriate.
*/
const clusterLivePhotos = (files: ClusterableFile[]) => {
const clusterLivePhotos = async (files: ClusterableFile[]) => {
const result: ClusteredFile[] = [];
const filesWithName: (ClusterableFile & { fileName: string })[] = files.map(
(f) => {
@ -799,17 +800,15 @@ const clusterLivePhotos = (files: ClusterableFile[]) => {
fileName: f.fileName,
fileType: fFileType,
collectionID: f.collectionID,
/* TODO(MR): ElectronFile changes */
size: (f as FileWithCollection).file.size,
fileOrPath: f.file,
};
const ga: PotentialLivePhotoAsset = {
fileName: g.fileName,
fileType: gFileType,
collectionID: g.collectionID,
/* TODO(MR): ElectronFile changes */
size: (g as FileWithCollection).file.size,
fileOrPath: g.file,
};
if (areLivePhotoAssets(fa, ga)) {
if (await areLivePhotoAssets(fa, ga)) {
result.push({
localID: f.localID,
collectionID: f.collectionID,
@ -841,10 +840,10 @@ interface PotentialLivePhotoAsset {
fileName: string;
fileType: FILE_TYPE;
collectionID: number;
size: number;
fileOrPath: File | string;
}
const areLivePhotoAssets = (
const areLivePhotoAssets = async (
f: PotentialLivePhotoAsset,
g: PotentialLivePhotoAsset,
) => {
@ -884,9 +883,11 @@ const areLivePhotoAssets = (
// we use doesn't support stream as a input.
const maxAssetSize = 20 * 1024 * 1024; /* 20MB */
if (f.size > maxAssetSize || g.size > maxAssetSize) {
const fSize = await fopSize(f.fileOrPath);
const gSize = await fopSize(g.fileOrPath);
if (fSize > maxAssetSize || gSize > maxAssetSize) {
log.info(
`Not classifying assets with too large sizes ${[f.size, g.size]} as a live photo`,
`Not classifying assets with too large sizes ${[fSize, gSize]} as a live photo`,
);
return false;
}

View file

@ -318,6 +318,17 @@ export const uploader = async (
}
};
/**
* Return the size of the given file
*
* @param fileOrPath The {@link File}, or the path to it. Note that it is only
* valid to specify a path if we are running in the context of our desktop app.
*/
export const fopSize = async (fileOrPath: File | string): Promise<number> =>
fileOrPath instanceof File
? fileOrPath.size
: await ensureElectron().fs.size(fileOrPath);
export const getFileName = (file: File | ElectronFile | string) =>
typeof file == "string" ? basename(file) : file.name;

View file

@ -189,6 +189,11 @@ export interface Electron {
* directory.
*/
isDir: (dirPath: string) => Promise<boolean>;
/**
* Return the size in bytes of the file at {@link path}.
*/
size: (path: string) => Promise<number>;
};
// - Conversion