-
Notifications
You must be signed in to change notification settings - Fork 0
feat: preview from source bus #66
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
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ddc4a6a
feat: preview from source bus
tripodsan f9f1b78
chore: make tests happy
tripodsan d2cc796
chore: update
tripodsan 1093b79
feat: add media handler
tripodsan ff7c6ca
Merge branch 'main' into preview-sourcebus
tripodsan 08c7670
chore: more tests
tripodsan f5827cc
Merge branch 'main' into preview-sourcebus
tripodsan 2a4e224
feat: add json support
tripodsan 7f8534d
feat: add sourcebus file support
tripodsan ac53696
wip
tripodsan cf23616
feat: add list
tripodsan 0cd6d43
chore: fix typo
tripodsan cb0bd3b
chore: cleanup
tripodsan 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,157 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { HelixStorage } from '@adobe/helix-shared-storage'; | ||
| import { MediaHandler, SizeTooLargeException } from '@adobe/helix-mediahandler'; | ||
| import { ConstraintsError, html2md, TooManyImagesError } from '@adobe/helix-html2md'; | ||
| import { Response } from '@adobe/fetch'; | ||
| import { errorResponse } from '../support/utils.js'; | ||
| import { error } from './errors.js'; | ||
|
|
||
| const DEFAULT_MAX_IMAGE_SIZE = 20 * 1024 * 1024; // 20mb | ||
|
|
||
| const DEFAULT_MAX_IMAGES = 200; | ||
|
|
||
| /** | ||
| * Retrieves a file from source bus. | ||
| * | ||
| * @param {import('../support/AdminContext').AdminContext} ctx context | ||
| * @param {import('../support/RequestInfo').RequestInfo} info request info | ||
| * @param {object} [opts] options | ||
| * @param {object} [opts.source] content source | ||
| * @param {string} [opts.lastModified] last modified | ||
| * @param {number} [opts.fetchTimeout] fetch timeout | ||
| * @returns {Promise<Response>} response | ||
| */ | ||
| async function handle(ctx, info, opts) { | ||
| const { config: { content, limits }, log } = ctx; | ||
|
|
||
| const source = opts?.source ?? content.source; | ||
| const sourceUrl = new URL(source.url); | ||
| // extract org and site from url.pathname, format: https://api.aem.live/<org>/sites/<site>/source | ||
| // e.g. /adobe/sites/foo/source | ||
| const pathMatch = sourceUrl.pathname.match(/^\/([^/]+)\/sites\/([^/]+)\/source$/); | ||
| if (!pathMatch) { | ||
| return errorResponse(log, 400, error( | ||
| 'Source url must be in the format: https://api.aem.live/<org>/sites/<site>/source. Got: $1', | ||
| sourceUrl.href, | ||
| )); | ||
| } | ||
| const [, org, site] = pathMatch; // eslint-disable-line prefer-destructuring | ||
|
|
||
| // for now, only allow source bus from the same org and site | ||
| if (org !== info.org || site !== info.site) { | ||
| return errorResponse(log, 400, error( | ||
| 'Source bus is not allowed for org: $1, site: $2', | ||
| org, | ||
| site, | ||
| )); | ||
| } | ||
| // the source is stored as .html files in the source bus | ||
| let sourcePath = info.resourcePath; | ||
| if (info.ext === '.md') { | ||
| sourcePath = `${sourcePath.substring(0, sourcePath.length - '.md'.length)}.html`; | ||
| /* c8 ignore next 7 */ | ||
| } else { | ||
| // this should never happen, since all resourcePaths are properly mapped before | ||
| return errorResponse(log, 400, error( | ||
| 'unexpected file extension: $1', | ||
| info.ext, | ||
| )); | ||
| } | ||
|
|
||
| // load content from source bus | ||
| const sourceBus = HelixStorage.fromContext(ctx).sourceBus(); | ||
| const meta = {}; | ||
| const body = await sourceBus.get(`${org}/${site}${sourcePath}`, meta); | ||
| if (!body) { | ||
| return new Response('', { status: 404 }); | ||
| } | ||
|
|
||
| const { | ||
| MEDIAHANDLER_NOCACHHE: noCache, | ||
| CLOUDFLARE_ACCOUNT_ID: r2AccountId, | ||
| CLOUDFLARE_R2_ACCESS_KEY_ID: r2AccessKeyId, | ||
| CLOUDFLARE_R2_SECRET_ACCESS_KEY: r2SecretAccessKey, | ||
| } = ctx.env; | ||
|
|
||
| const mediaHandler = new MediaHandler({ | ||
| r2AccountId, | ||
| r2AccessKeyId, | ||
| r2SecretAccessKey, | ||
| bucketId: ctx.attributes.bucketMap.media, | ||
| owner: org, | ||
| repo: site, | ||
| ref: 'main', | ||
| contentBusId: content.contentBusId, | ||
| log, | ||
| noCache, | ||
| fetchTimeout: 5000, // limit image fetches to 5s | ||
| forceHttp1: true, | ||
| maxSize: limits?.html2md?.maxImageSize ?? DEFAULT_MAX_IMAGE_SIZE, | ||
| }); | ||
|
|
||
| const maxImages = limits?.html2md?.maxImages ?? DEFAULT_MAX_IMAGES; | ||
| try { | ||
| // convert to md | ||
| const md = await html2md(body, { | ||
| mediaHandler, | ||
| log, | ||
| url: sourceUrl.href + sourcePath, // only used for logging | ||
| org, | ||
| site, | ||
| unspreadLists: true, | ||
| maxImages, | ||
| externalImageUrlPrefixes: [`https://main--${site}--${org}.aem.page/`], | ||
| }); | ||
|
|
||
| return new Response(md, { | ||
| status: 200, | ||
| headers: { | ||
| 'content-type': 'text/markdown', | ||
| 'last-modified': meta.LastModified?.toUTCString(), | ||
| }, | ||
| }); | ||
| } catch (e) { | ||
| if (e instanceof TooManyImagesError) { | ||
| return errorResponse(log, 409, error( | ||
| 'Unable to preview \'$1\': Documents has more than $2 images: $3', | ||
| sourcePath, | ||
| maxImages, | ||
| e.message, // todo: include num images in error | ||
| )); | ||
| } | ||
| if (e instanceof SizeTooLargeException) { | ||
| return errorResponse(log, 409, error( | ||
| 'Unable to preview \'$1\': $2', | ||
| sourcePath, | ||
| e.message, | ||
| )); | ||
| } | ||
| /* c8 ignore next 6 */ | ||
| return errorResponse(log, 500, error( | ||
| 'Unable to preview \'$1\': $2', | ||
| sourcePath, | ||
| e.message, | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @type {import('./contentproxy.js').ContentSourceHandler} | ||
| */ | ||
| export default { | ||
| name: 'sourcebus', | ||
| handle, | ||
| handleJSON: () => { throw new Error('not implemented'); }, | ||
| handleFile: () => { throw new Error('not implemented'); }, | ||
| list: () => { throw new Error('not implemented'); }, | ||
| }; | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,14 @@ | ||
| <html> | ||
| <head></head> | ||
| <body> | ||
| <main> | ||
| <div> | ||
| <h1>Hello, world.</h1> | ||
| <p>source bus images.</p> | ||
| <img src="https://www.example.com/image1.jpg"> | ||
| <img src="https://www.example.com/image2.jpg"> | ||
| <img src="https://main--site--org.aem.page/media_2c2e2c6c049ccf4b583431e14919687f3a39cc227.png#width=300&height=300"> | ||
| </div> | ||
| </main> | ||
| </body> | ||
| </html> |
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,11 @@ | ||
| <html> | ||
| <head></head> | ||
| <body> | ||
| <main> | ||
| <div> | ||
| <h1>Hello, world.</h1> | ||
| <p>Testing, source bus.</p> | ||
| </div> | ||
| </main> | ||
| </body> | ||
| </html> |
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,11 @@ | ||
| <html> | ||
| <head></head> | ||
| <body> | ||
| <main> | ||
| <div> | ||
| <h1>Hello, world.</h1> | ||
| <p>Testing, source bus.</p> | ||
| </div> | ||
| </main> | ||
| </body> | ||
| </html> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MEDIAHANDLER_NOCACHHEis probably a typo. (Two 'H's)