fix: more tests more better
This commit is contained in:
@@ -45,11 +45,37 @@ export class MediaEngine extends EventEmitter {
|
||||
super();
|
||||
}
|
||||
|
||||
private getMediaDir(): string {
|
||||
private getMediaBaseDir(): string {
|
||||
const userDataPath = app.getPath('userData');
|
||||
return path.join(userDataPath, 'projects', this.currentProjectId, 'media');
|
||||
}
|
||||
|
||||
private getMediaDir(): string {
|
||||
// Kept for backwards compatibility - returns base media directory
|
||||
return this.getMediaBaseDir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date-based directory for media based on its creation date.
|
||||
* Format: media/YYYY/MM/
|
||||
*/
|
||||
private getMediaDirForDate(date: Date): string {
|
||||
const baseDir = this.getMediaBaseDir();
|
||||
const year = date.getFullYear().toString();
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
||||
return path.join(baseDir, year, month);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full path for a media file based on id, extension, and date.
|
||||
* Returns: media/YYYY/MM/{id}.{ext}
|
||||
*/
|
||||
getMediaPathForDate(id: string, ext: string, date: Date): string {
|
||||
const dir = this.getMediaDirForDate(date);
|
||||
const extension = ext.startsWith('.') ? ext : `.${ext}`;
|
||||
return path.join(dir, `${id}${extension}`);
|
||||
}
|
||||
|
||||
setProjectContext(projectId: string): void {
|
||||
this.currentProjectId = projectId;
|
||||
}
|
||||
@@ -204,12 +230,14 @@ export class MediaEngine extends EventEmitter {
|
||||
const originalName = path.basename(sourcePath);
|
||||
const ext = path.extname(originalName);
|
||||
const filename = `${id}${ext}`;
|
||||
const mediaDir = this.getMediaDir();
|
||||
|
||||
// Use date-based directory structure (media/YYYY/MM/)
|
||||
const mediaDir = this.getMediaDirForDate(now);
|
||||
await fs.mkdir(mediaDir, { recursive: true });
|
||||
const destPath = path.join(mediaDir, filename);
|
||||
|
||||
// Copy file to media directory
|
||||
await fs.writeFile(destPath, sourceBuffer);
|
||||
await fs.copyFile(sourcePath, destPath);
|
||||
|
||||
const mediaData: MediaData = {
|
||||
id,
|
||||
|
||||
@@ -67,11 +67,36 @@ export class PostEngine extends EventEmitter {
|
||||
super();
|
||||
}
|
||||
|
||||
private getPostsDir(): string {
|
||||
private getPostsBaseDir(): string {
|
||||
const userDataPath = app.getPath('userData');
|
||||
return path.join(userDataPath, 'projects', this.currentProjectId, 'posts');
|
||||
}
|
||||
|
||||
private getPostsDir(): string {
|
||||
// Kept for backwards compatibility - returns base posts directory
|
||||
return this.getPostsBaseDir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date-based directory for a post based on its creation date.
|
||||
* Format: posts/YYYY/MM/
|
||||
*/
|
||||
private getPostsDirForDate(date: Date): string {
|
||||
const baseDir = this.getPostsBaseDir();
|
||||
const year = date.getFullYear().toString();
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
||||
return path.join(baseDir, year, month);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full path for a post file based on slug and date.
|
||||
* Returns: posts/YYYY/MM/{slug}.md
|
||||
*/
|
||||
getPostPath(slug: string, date: Date): string {
|
||||
const dir = this.getPostsDirForDate(date);
|
||||
return path.join(dir, `${slug}.md`);
|
||||
}
|
||||
|
||||
setProjectContext(projectId: string): void {
|
||||
this.currentProjectId = projectId;
|
||||
}
|
||||
@@ -109,7 +134,8 @@ export class PostEngine extends EventEmitter {
|
||||
if (post.author) metadata.author = post.author;
|
||||
if (post.publishedAt) metadata.publishedAt = post.publishedAt.toISOString();
|
||||
|
||||
const postsDir = this.getPostsDir();
|
||||
// Use date-based directory structure (posts/YYYY/MM/)
|
||||
const postsDir = this.getPostsDirForDate(post.createdAt);
|
||||
await fs.mkdir(postsDir, { recursive: true });
|
||||
|
||||
const fileContent = matter.stringify(post.content, metadata);
|
||||
|
||||
Reference in New Issue
Block a user