Skip to content

Add feature to automatically convert external images to S3 (#41)#43

Open
l3ickey wants to merge 5 commits into
jvsteiner:mainfrom
l3ickey:feature/replace-downloaded-attachments
Open

Add feature to automatically convert external images to S3 (#41)#43
l3ickey wants to merge 5 commits into
jvsteiner:mainfrom
l3ickey:feature/replace-downloaded-attachments

Conversation

@l3ickey

@l3ickey l3ickey commented May 14, 2025

Copy link
Copy Markdown

This PR adds a new feature to automatically upload external image links in Obsidian notes to S3 and replace them.

Key changes:

  • Add a new command "Convert External Images to S3"
  • Execute the process in 3 stages:
     1. Replace ALT text in external image links with file hash
     2. Run Obsidian's "Download attachments for current file" command
     3. Upload downloaded images to S3 and replace original links
  • Implement automatic detection of download completion
  • Automatically remove duplicate S3 links

Example of the transformation process:

Initial external image: ![image](https://example.com/img.png)

  1. After Step 1 (hash generation): ![a82b844446ae6b9bb95c47d806265e4a.png](https://example.com/img.png)
  2. After Step 2 (download): Both ![[img.png|a82b844446ae6b9bb95c47d806265e4a.png]] and ![image](https://obsidian-bucket.s3.ap-northeast-1.amazonaws.com/a82b844446ae6b9bb95c47d806265e4a.png) exist in the document
  3. After Step 3 (cleanup): Only ![image](https://obsidian-bucket.s3.ap-northeast-1.amazonaws.com/a82b844446ae6b9bb95c47d806265e4a.png) remains

This feature makes it easy to migrate notes containing image links from other sources to S3 hosting.

@jvsteiner

Copy link
Copy Markdown
Owner

It would be a lot easier for me to review this if you could squash these into a single commit.

@jvsteiner

Copy link
Copy Markdown
Owner

I have some concerns:

Architecture:

  • Relies on Obsidian's internal (this.app as any).commands.executeCommandById — undocumented API
    that could break
  • Polling loop (30s timeout, 1s intervals) to detect download completion is fragile; there's no
    event-based way to know when Obsidian finishes downloading

Code quality:

  • Significant code duplication — the editor-finding logic (search leaves, fall back to active view)
    is repeated in processDownloadedImages, replaceAltWithHash, and could be extracted to a helper
  • The s3LinkRegex is named "S3" but matches any http(s) URL — misleading naming
  • error.message used without type narrowing (TypeScript strict mode issue)
  • Several console.log calls that look like debug output left in

Robustness:

  • editor.setValue() replaces the entire document, which can lose undo history and is generally
    discouraged in Obsidian plugins — editor.replaceRange() is preferred
  • The hash-matching logic assumes filenames are formatted as hash.extension, which could collide
    with unrelated content

Minor:

  • Whitespace-only changes to onunload() { } and the ternary formatting are noise — my earlier
    comment about squashing commits applies here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants