obsidian#DataAdapter TypeScript Examples

The following examples show how to use obsidian#DataAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: replaceImages.ts    From obsidian-ReadItLater with MIT License 5 votes vote down vote up
async function chooseFileName(
    adapter: DataAdapter,
    dir: string,
    baseName: string,
    link: string,
    contentData: ArrayBuffer,
    fileExtension: string | false,
): Promise<{ fileName: string; needWrite: boolean }> {
    if (!fileExtension) {
        return { fileName: '', needWrite: false };
    }
    // if there is no anchor try get file name from url
    if (!baseName) {
        const parsedUrl = new URL(link);

        baseName = basename(parsedUrl.pathname);
    }
    // if there is no part for file name from url use name template
    if (!baseName) {
        baseName = FILENAME_TEMPLATE;
    }

    // if filename already ends with correct extension, remove it to work with base name
    if (baseName.endsWith(`.${fileExtension}`)) {
        baseName = baseName.slice(0, -1 * (fileExtension.length + 1));
    }

    baseName = normalizeFilename(baseName);

    let fileName = '';
    let needWrite = true;
    let index = 0;
    while (!fileName && index < MAX_FILENAME_INDEX) {
        const suggestedName = index
            ? pathJoin(dir, `${baseName}-${index}.${fileExtension}`)
            : pathJoin(dir, `${baseName}.${fileExtension}`);

        if (await adapter.exists(suggestedName, false)) {
            linkHashes.ensureHashGenerated(link, contentData);

            const fileData = await adapter.readBinary(suggestedName);

            if (linkHashes.isSame(link, fileData)) {
                fileName = suggestedName;
                needWrite = false;
            }
        } else {
            fileName = suggestedName;
        }

        index++;
    }
    if (!fileName) {
        throw new Error('Failed to generate file name for media file.');
    }

    linkHashes.ensureHashGenerated(link, contentData);

    return { fileName, needWrite };
}
Example #2
Source File: main.ts    From obsidian-readwise with GNU General Public License v3.0 5 votes vote down vote up
fs: DataAdapter;