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

chore(server,cli,web): housekeeping and stricter code style #6751

Merged
merged 37 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d00cae2
add unicorn to eslint
etnoy Jan 30, 2024
37b4a2f
fix lint errors for cli
etnoy Jan 30, 2024
588900a
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Jan 30, 2024
bd3a018
fix merge
etnoy Jan 30, 2024
65f0b73
fix album name extraction
etnoy Jan 30, 2024
de12ccf
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Jan 30, 2024
ccb10a5
Update cli/src/commands/upload.command.ts
etnoy Jan 30, 2024
0f6d62c
es2k23
etnoy Jan 30, 2024
d2c4db1
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Jan 31, 2024
c12ae01
use lowercase os
etnoy Jan 31, 2024
9b2d695
return undefined album name
etnoy Jan 31, 2024
37969e4
fix bug in asset response dto
etnoy Jan 31, 2024
8d7512c
auto fix issues
etnoy Jan 31, 2024
8e169fd
fix server code style
etnoy Jan 31, 2024
a7c4142
es2022 and formatting
etnoy Jan 31, 2024
f34e461
fix compilation error
etnoy Jan 31, 2024
0d1adf7
fix test
etnoy Jan 31, 2024
7870965
fix config load
etnoy Jan 31, 2024
5794330
fix last lint errors
etnoy Jan 31, 2024
d34467d
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Jan 31, 2024
d4c5afb
set string type
etnoy Jan 31, 2024
f18d061
bump ts
etnoy Jan 31, 2024
8fad3f1
start work on web
etnoy Jan 31, 2024
7b8007d
web formatting
etnoy Jan 31, 2024
b340ba8
Fix UUIDParamDto as UUIDParamDto
etnoy Feb 1, 2024
b1e4f0b
fix library service lint
etnoy Feb 1, 2024
2d5aec9
fix web errors
etnoy Feb 1, 2024
09537b5
fix errors
etnoy Feb 1, 2024
1ab1106
formatting
etnoy Feb 1, 2024
e80c92b
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Feb 1, 2024
743ffff
wip
etnoy Feb 1, 2024
796edad
Merge branch 'main' of https://github.com/immich-app/immich into chor…
etnoy Feb 1, 2024
a1d09e0
lints fixed
etnoy Feb 1, 2024
f6626af
web can now start
etnoy Feb 1, 2024
626544e
alphabetical package json
etnoy Feb 1, 2024
c9d11ca
rename error
etnoy Feb 2, 2024
69e0706
chore: clean up
jrasm91 Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion cli/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
tsconfigRootDir: __dirname,
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', 'plugin:unicorn/recommended'],
root: true,
env: {
node: true,
Expand All @@ -19,6 +19,14 @@ module.exports = {
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-floating-promises': 'error',
'unicorn/prefer-module': 'off',
curly: 2,
'prettier/prettier': 0,
'unicorn/prevent-abbreviations': [
'error',
{
ignore: ['\\.e2e-spec$', /^ignore/i],
},
],
},
};
30 changes: 19 additions & 11 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@
"@types/cli-progress": "^3.11.0",
"@types/mock-fs": "^4.13.1",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"eslint": "^8.56.0",
etnoy marked this conversation as resolved.
Show resolved Hide resolved
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^50.0.1",
"@vitest/coverage-v8": "^1.2.2",
"eslint": "^8.43.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-unicorn": "^50.0.0",
"immich": "file:../server",
"mock-fs": "^5.2.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.3",
"typescript": "^5.0.0",
"typescript": "^5.3.3",
"vitest": "^1.2.1"
},
"scripts": {
Expand Down
93 changes: 44 additions & 49 deletions cli/src/commands/upload.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BaseCommand } from './base-command';
import { basename } from 'node:path';
import { access, constants, stat, unlink } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import Os from 'os';
import os from 'node:os';

class Asset {
readonly path: string;
Expand All @@ -27,25 +27,31 @@ class Asset {

async prepare() {
const stats = await stat(this.path);
this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replace(/\s+/g, '');
this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replaceAll(/\s+/g, '');
this.fileCreatedAt = stats.mtime.toISOString();
this.fileModifiedAt = stats.mtime.toISOString();
this.fileSize = stats.size;
this.albumName = this.extractAlbumName();
}

async getUploadFormData(): Promise<FormData> {
if (!this.deviceAssetId) throw new Error('Device asset id not set');
if (!this.fileCreatedAt) throw new Error('File created at not set');
if (!this.fileModifiedAt) throw new Error('File modified at not set');
if (!this.deviceAssetId) {
throw new Error('Device asset id not set');
}
if (!this.fileCreatedAt) {
throw new Error('File created at not set');
}
if (!this.fileModifiedAt) {
throw new Error('File modified at not set');
}

// TODO: doesn't xmp replace the file extension? Will need investigation
const sideCarPath = `${this.path}.xmp`;
let sidecarData: ReadStream | undefined = undefined;
try {
await access(sideCarPath, constants.R_OK);
sidecarData = createReadStream(sideCarPath);
} catch (error) {}
} catch {}

const data: any = {
assetData: createReadStream(this.path),
Expand All @@ -57,8 +63,8 @@ class Asset {
};
const formData = new FormData();

for (const prop in data) {
formData.append(prop, data[prop]);
for (const property in data) {
formData.append(property, data[property]);
}

if (sidecarData) {
Expand Down Expand Up @@ -86,12 +92,8 @@ class Asset {
return await sha1(this.path);
}

private extractAlbumName(): string {
if (Os.platform() === 'win32') {
return this.path.split('\\').slice(-2)[0];
} else {
return this.path.split('/').slice(-2)[0];
}
private extractAlbumName(): string | undefined {
return os.platform() === 'win32' ? this.path.split('\\').at(-2) : this.path.split('/').at(-2);
}
}

Expand Down Expand Up @@ -162,7 +164,8 @@ export class UploadCommand extends BaseCommand {
}
}

const existingAlbums = (await this.immichApi.albumApi.getAllAlbums()).data;
const allAlbums = await this.immichApi.albumApi.getAllAlbums();
const existingAlbums = allAlbums.data;

uploadProgress.start(totalSize, 0);
uploadProgress.update({ value_formatted: 0, total_formatted: byteSize(totalSize) });
Expand Down Expand Up @@ -195,32 +198,30 @@ export class UploadCommand extends BaseCommand {
skipAsset = skipUpload && !isDuplicate;
}

if (!skipAsset) {
if (!options.dryRun) {
if (!skipUpload) {
const formData = await asset.getUploadFormData();
const res = await this.uploadAsset(formData);
existingAssetId = res.data.id;
uploadCounter++;
totalSizeUploaded += asset.fileSize;
if (!skipAsset && !options.dryRun) {
if (!skipUpload) {
const formData = await asset.getUploadFormData();
const response = await this.uploadAsset(formData);
existingAssetId = response.data.id;
uploadCounter++;
totalSizeUploaded += asset.fileSize;
}

if ((options.album || options.albumName) && asset.albumName !== undefined) {
let album = existingAlbums.find((album) => album.albumName === asset.albumName);
if (!album) {
const response = await this.immichApi.albumApi.createAlbum({
createAlbumDto: { albumName: asset.albumName },
});
album = response.data;
existingAlbums.push(album);
}

if ((options.album || options.albumName) && asset.albumName !== undefined) {
let album = existingAlbums.find((album) => album.albumName === asset.albumName);
if (!album) {
const res = await this.immichApi.albumApi.createAlbum({
createAlbumDto: { albumName: asset.albumName },
});
album = res.data;
existingAlbums.push(album);
}

if (existingAssetId) {
await this.immichApi.albumApi.addAssetsToAlbum({
id: album.id,
bulkIdsDto: { ids: [existingAssetId] },
});
}
if (existingAssetId) {
await this.immichApi.albumApi.addAssetsToAlbum({
id: album.id,
bulkIdsDto: { ids: [existingAssetId] },
});
}
}
}
Expand All @@ -233,12 +234,7 @@ export class UploadCommand extends BaseCommand {
uploadProgress.stop();
}

let messageStart;
if (options.dryRun) {
messageStart = 'Would have';
} else {
messageStart = 'Successfully';
}
const messageStart = options.dryRun ? 'Would have' : 'Successfully';

if (uploadCounter === 0) {
console.log('All assets were already uploaded, nothing to do.');
Expand Down Expand Up @@ -276,12 +272,11 @@ export class UploadCommand extends BaseCommand {
'x-api-key': this.immichApi.apiKey,
...data.getHeaders(),
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
maxContentLength: Number.POSITIVE_INFINITY,
maxBodyLength: Number.POSITIVE_INFINITY,
data,
};

const res = await axios(config);
return res;
return await axios(config);
}
}
8 changes: 4 additions & 4 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#! /usr/bin/env node
import { Command, Option } from 'commander';
import path from 'node:path';
import os from 'os';
import os from 'node:os';
import { version } from '../package.json';
import { LoginCommand } from './commands/login';
import { LogoutCommand } from './commands/logout.command';
import { ServerInfoCommand } from './commands/server-info.command';
import { UploadCommand } from './commands/upload.command';

const userHomeDir = os.homedir();
const configDir = path.join(userHomeDir, '.config/immich/');
const homeDirectory = os.homedir();
const configDirectory = path.join(homeDirectory, '.config/immich/');

const program = new Command()
.name('immich')
.version(version)
.description('Command line interface for Immich')
.addOption(new Option('-d, --config', 'Configuration directory').env('IMMICH_CONFIG_DIR').default(configDir));
.addOption(new Option('-d, --config', 'Configuration directory').env('IMMICH_CONFIG_DIR').default(configDirectory));

program
.command('upload')
Expand Down
10 changes: 4 additions & 6 deletions cli/src/services/crawl.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { glob } from 'glob';
import * as fs from 'fs';
import * as fs from 'node:fs';

export class CrawlOptions {
pathsToCrawl!: string[];
Expand All @@ -12,14 +12,14 @@ export class CrawlService {
private readonly extensions!: string[];

constructor(image: string[], video: string[]) {
this.extensions = image.concat(video).map((extension) => extension.replace('.', ''));
this.extensions = [...image, ...video].map((extension) => extension.replace('.', ''));
}

async crawl(options: CrawlOptions): Promise<string[]> {
const { recursive, pathsToCrawl, exclusionPatterns, includeHidden } = options;

if (!pathsToCrawl) {
return Promise.resolve([]);
return [];
}

const patterns: string[] = [];
Expand Down Expand Up @@ -65,8 +65,6 @@ export class CrawlService {
ignore: exclusionPatterns,
});

const returnedFiles = crawledFiles.concat(globbedFiles);
returnedFiles.sort();
return returnedFiles;
return [...crawledFiles, ...globbedFiles].sort();
}
}