Skip to content

Commit

Permalink
feat: support variable
Browse files Browse the repository at this point in the history
  • Loading branch information
trganda committed Sep 14, 2023
1 parent e782ede commit 72c1da7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
11 changes: 5 additions & 6 deletions src/arrange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ export class ArrangeHandler {
return;
}

const metadata = getMetadata(obNote);
const attachPath = metadata.getAttachmentPath(setting);

for (let link of attachments[obNote]) {
try {
link = decodeURI(link);
Expand All @@ -77,12 +74,14 @@ export class ArrangeHandler {
continue;
}

const attachName = metadata.getAttachFileName(
const metadata = getMetadata(obNote, linkFile);
const attachPath = metadata.getAttachmentPath(setting);

const attachName = await metadata.getAttachFileName(
setting,
this.settings.dateFormat,
"",
MD5(linkFile),
linkFile.extension,
this.app.vault.adapter,
path.basename(link, path.extname(link))
);
// debugLog(`rearrangeAttachment - ${attachPath}, ${attachName}`);
Expand Down
4 changes: 2 additions & 2 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CreateHandler {

debugLog("processAttach - active file path", activeFile.path);

const metadata = getMetadata(activeFile.path);
const metadata = getMetadata(activeFile.path, file);
debugLog("processAttach - metadata:", metadata);

// const attachName =
Expand All @@ -55,7 +55,7 @@ export class CreateHandler {
// const attachPath = getAttachmentPath(activeFile.basename, parentPath, parentName, setting);
const attachPath = metadata.getAttachmentPath(setting);
const attachName =
metadata.getAttachFileName(setting, this.settings.dateFormat, file.basename, MD5(file), file.extension) + "." + file.extension;
await metadata.getAttachFileName(setting, this.settings.dateFormat, file.basename, this.app.vault.adapter) + "." + file.extension;

// make sure the path was created
if (!(await this.app.vault.adapter.exists(attachPath, true))) {
Expand Down
45 changes: 34 additions & 11 deletions src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizePath } from "obsidian";
import { DataAdapter, TFile, normalizePath } from "obsidian";
import { AttachmentPathSettings } from "./settings/settings";
import {
SETTINGS_VARIABLES_DATES,
Expand All @@ -11,6 +11,7 @@ import {
} from "./lib/constant";
import { getRootPath } from "./commons";
import { path } from "./lib/path";
import { MD5 } from "./utils";

/**
* Metadata of notes file
Expand All @@ -34,13 +35,24 @@ class Metadata {
/** parent path basename of file */
parentName = "";

constructor(path: string, name: string, basename: string, extension: string, parentPath: string, parentName: string) {
attachmentFile: TFile;

constructor(
path: string,
name: string,
basename: string,
extension: string,
parentPath: string,
parentName: string,
attachmentFile: TFile
) {
this.path = path;
this.name = name;
this.basename = basename;
this.extension = extension;
this.parentPath = parentPath;
this.parentName = parentName;
this.attachmentFile = attachmentFile;
}

/**
Expand All @@ -52,25 +64,32 @@ class Metadata {
* @param {string} [linkName] - optional name for the attachment link
* @return {string} the formatted attachment file name
*/
getAttachFileName(setting: AttachmentPathSettings, dateFormat: string, originalName: string, md5: string, extension: string, linkName?: string) {
async getAttachFileName(
setting: AttachmentPathSettings,
dateFormat: string,
originalName: string,
adapter: DataAdapter,
linkName?: string
) {
const dateTime = window.moment().format(dateFormat);
const md5 = await MD5(adapter, this.attachmentFile);
// we have no persistence of original name, return current linking name
if (setting.attachFormat.includes(SETTINGS_VARIABLES_ORIGINALNAME)) {
if (originalName === "" && linkName != undefined) {
return linkName;
} else {
return setting.attachFormat
.replace(`${SETTINGS_VARIABLES_DATES}`, dateTime)
.replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename)
.replace(`${SETTINGS_VARIABLES_ORIGINALNAME}`, originalName)
.replace(`${SETTINGS_VARIABLES_EXTENSION}`, extension)
.replace(`${SETTINGS_VARIABLES_MD5}`, md5);
.replace(`${SETTINGS_VARIABLES_DATES}`, dateTime)
.replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename)
.replace(`${SETTINGS_VARIABLES_ORIGINALNAME}`, originalName)
.replace(`${SETTINGS_VARIABLES_EXTENSION}`, this.attachmentFile.extension)
.replace(`${SETTINGS_VARIABLES_MD5}`, md5);
}
}
return setting.attachFormat
.replace(`${SETTINGS_VARIABLES_DATES}`, dateTime)
.replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename)
.replace(`${SETTINGS_VARIABLES_EXTENSION}`, extension)
.replace(`${SETTINGS_VARIABLES_EXTENSION}`, this.attachmentFile.extension)
.replace(`${SETTINGS_VARIABLES_MD5}`, md5);
}

Expand Down Expand Up @@ -99,12 +118,16 @@ class Metadata {
* @param {string} file - The full path to the file.
* @return {Metadata} A new instance of Metadata containing information about the file.
*/
export function getMetadata(file: string): Metadata {
export function getMetadata(file: string, attach?: TFile): Metadata {
const parentPath = path.dirname(file);
const parentName = path.basename(parentPath);
const name = path.basename(file);
const extension = path.extname(file);
const basename = path.basename(file, extension);

return new Metadata(file, name, basename, extension, parentPath, parentName);
if (attach === undefined) {
attach = new TFile();
}

return new Metadata(file, name, basename, extension, parentPath, parentName, attach);
}
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TAbstractFile, TFile } from "obsidian";
import { DataAdapter, TAbstractFile, TFile } from "obsidian";
import { AttachmentManagementPluginSettings, AttachmentPathSettings } from "./settings/settings";
import {
SETTINGS_VARIABLES_NOTENAME,
Expand Down Expand Up @@ -178,11 +178,15 @@ export function getParentFolder(rf: TFile) {
return { parentPath, parentName };
}

export function MD5(file: TFile): string {
export async function MD5(adapter:DataAdapter, file: TFile): Promise<string> {
const md5 = new Md5();
const content = this.app.vault.readBinary(file)
md5.appendByteArray(new Uint8Array(content));

if (!(adapter.exists(file.path, true))) {
return "";
}

const content = await adapter.readBinary(file.path);
md5.appendByteArray(new Uint8Array(content));
const ret = md5.end() as string;

return ret.toUpperCase();
Expand Down

0 comments on commit 72c1da7

Please sign in to comment.