-
Notifications
You must be signed in to change notification settings - Fork 19
fix: init media upload via drag and drop #18
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab33913
fix: init media upload via drag and drop
farnabaz 5fcfb8c
lint: fix
farnabaz 81b25ef
feat: revert and upload in context
farnabaz 4d8db7f
feat: studio service worker to preview uploaded medias
farnabaz 1cf6deb
feat: tree item preview
farnabaz d797136
fix: service worker
farnabaz f231420
lint: fix
farnabaz 81595b7
Apply suggestions from code review
farnabaz c8a4859
fix: typo
farnabaz 970e0c7
chore: apply suggestion
farnabaz cf79b13
Merge branch 'main' into fix/media
larbish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| export const serviceWorker = () => ` | ||
| const EXTENSIONS_WITH_PREVIEW = new Set([ | ||
| 'jpg', | ||
| 'jpeg', | ||
| 'png', | ||
| 'gif', | ||
| 'webp', | ||
| 'ico', | ||
| 'avif', | ||
| ]) | ||
| self.addEventListener('fetch', event => { | ||
| const url = new URL(event.request.url); | ||
| const isSameDomain = url.origin === self.location.origin; | ||
|
|
||
| if (!isSameDomain) { | ||
| return event.respondWith(fetch(event.request)); | ||
| } | ||
|
|
||
| if (url.pathname.startsWith('/_ipx/_/') || EXTENSIONS_WITH_PREVIEW.has(url.pathname.split('.').pop())) { | ||
| console.log('Fetching from IndexedDB:', url.pathname); | ||
| return event.respondWith(fetchFromIndexedDB(event, url)); | ||
| } | ||
|
|
||
| event.respondWith(fetch(event.request)) | ||
| }) | ||
|
|
||
| function fetchFromIndexedDB(event, url) { | ||
| const dbKey = ['public-assets:', url.pathname.replace(/^\\/+(_ipx\\/_\\/)?/, '').replace('/', ':')].join('') | ||
| return getData(dbKey).then(data => { | ||
| if (!data) { | ||
| console.log('No data found in IndexedDB:', url.pathnam, dbKey); | ||
| return fetch(event.request); | ||
| } | ||
|
|
||
| const json = JSON.parse(data) | ||
| const parsed = parseDataUrl(json.modified.raw); | ||
| const bytes = base64ToUint8Array(parsed.base64); | ||
|
|
||
| return new Response(bytes, { | ||
| headers: { 'Content-Type': parsed.mime } | ||
| }); | ||
| }) | ||
| } | ||
|
|
||
| function parseDataUrl(dataUrl) { | ||
| // Example: data:image/png;base64,iVBORw0KG... | ||
| const match = dataUrl.match(/^data:(.+);base64,(.+)$/); | ||
| if (!match) return null; | ||
| return { | ||
| mime: match[1], | ||
| base64: match[2] | ||
| }; | ||
| } | ||
|
|
||
| function base64ToUint8Array(base64) { | ||
| const binary = atob(base64); | ||
| const len = binary.length; | ||
| const bytes = new Uint8Array(len); | ||
| for (let i = 0; i < len; i++) { | ||
| bytes[i] = binary.charCodeAt(i); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| // IndexedDB | ||
| function openDB() { | ||
| return new Promise((resolve, reject) => { | ||
| const request = indexedDB.open('nuxt-content-studio-media', 1); | ||
| request.onupgradeneeded = event => { | ||
| const db = event.target.result; | ||
| db.createObjectStore('drafts', { keyPath: 'id' }); | ||
| }; | ||
| request.onsuccess = event => resolve(event.target.result); | ||
| request.onerror = event => reject(event.target.error); | ||
| }); | ||
| } | ||
|
|
||
| // Read data from the object store | ||
| function getData(key) { | ||
| return openDB().then(db => { | ||
| return new Promise((resolve, reject) => { | ||
| const tx = db.transaction('drafts', 'readonly'); | ||
| const store = tx.objectStore('drafts'); | ||
| const request = store.get(key); | ||
| request.onsuccess = () => resolve(request.result); | ||
| request.onerror = () => reject(request.error); | ||
| }); | ||
| }); | ||
| } | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.