Skip to content

feat: add album start and end dates for storage template #17188

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

Merged
merged 7 commits into from
Apr 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions server/src/services/storage-template.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ interface RenderMetadata {
filename: string;
extension: string;
albumName: string | null;
albumCreatedAt: Date | null;
albumUpdatedAt: Date | null;
albumStartDate: Date | null;
albumEndDate: Date | null;
}

@Injectable()
Expand Down Expand Up @@ -99,6 +103,10 @@ export class StorageTemplateService extends BaseService {
filename: 'IMG_123',
extension: 'jpg',
albumName: 'album',
albumCreatedAt: new Date(),
albumUpdatedAt: new Date(),
albumStartDate: new Date(),
albumEndDate: new Date()
});
} catch (error) {
this.logger.warn(`Storage template validation failed: ${JSON.stringify(error)}`);
Expand Down Expand Up @@ -253,16 +261,32 @@ export class StorageTemplateService extends BaseService {
}

let albumName = null;
let albumCreatedAt = null;
let albumUpdatedAt = null;
let albumStartDate = null;
let albumEndDate = null;
if (this.template.needsAlbum) {
const albums = await this.albumRepository.getByAssetId(asset.ownerId, asset.id);
albumName = albums?.[0]?.albumName || null;
const album = albums?.[0];
if (album) {
const [metadata] = await this.albumRepository.getMetadataForIds([album.id])
albumName = album.albumName || null;
albumCreatedAt = album.createdAt || null;
albumUpdatedAt = album.updatedAt || null;
albumStartDate = metadata?.startDate || null;
albumEndDate = metadata?.endDate || null;
}
}

const storagePath = this.render(this.template.compiled, {
asset,
filename: sanitized,
extension,
albumName,
albumCreatedAt,
albumUpdatedAt,
albumStartDate,
albumEndDate,
});
const fullPath = path.normalize(path.join(rootPath, storagePath));
let destination = `${fullPath}.${extension}`;
Expand Down Expand Up @@ -321,12 +345,12 @@ export class StorageTemplateService extends BaseService {
return {
raw: template,
compiled: handlebar.compile(template, { knownHelpers: undefined, strict: true }),
needsAlbum: template.includes('{{album}}'),
needsAlbum: template.includes('album'),
};
}

private render(template: HandlebarsTemplateDelegate<any>, options: RenderMetadata) {
const { filename, extension, asset, albumName } = options;
const { filename, extension, asset, albumName, albumCreatedAt, albumUpdatedAt, albumStartDate, albumEndDate } = options;
const substitutions: Record<string, string> = {
filename,
ext: extension,
Expand All @@ -344,6 +368,13 @@ export class StorageTemplateService extends BaseService {

for (const token of Object.values(storageTokens).flat()) {
substitutions[token] = dt.toFormat(token);
if (albumName) {
// Use system time zone for album dates to ensure all assets get the exact same date.
substitutions["album-createdAt-" + token] = albumCreatedAt ? DateTime.fromJSDate(albumCreatedAt, { zone: systemTimeZone }).toFormat(token) : '';
substitutions["album-updatedAt-" + token] = albumUpdatedAt ? DateTime.fromJSDate(albumUpdatedAt, { zone: systemTimeZone }).toFormat(token) : '';
substitutions["album-startDate-" + token] = albumStartDate ? DateTime.fromJSDate(albumStartDate, { zone: systemTimeZone }).toFormat(token) : '';
substitutions["album-endDate-" + token] = albumEndDate ? DateTime.fromJSDate(albumEndDate, { zone: systemTimeZone }).toFormat(token) : '';
}
}

return template(substitutions).replaceAll(/\/{2,}/gm, '/');
Expand Down
Loading