-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Add support for 3D/CAD file formats preview #34794
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
wxiaoguang
merged 24 commits into
go-gitea:main
from
kerwin612:feat/support-preview-3d-file
Jun 30, 2025
Merged
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
448effb
feat: add plugin-based file rendering system with 3D file preview sup…
kerwin612 2e80917
fix tests error
kerwin612 1296d61
Merge branch 'main' into feat/support-preview-3d-file
wxiaoguang 5ef2279
Merge branch 'main' into feat/support-preview-3d-file
kerwin612 c84be59
fix
kerwin612 f32eaac
Merge remote-tracking branch 'kerwin612/feat/support-preview-3d-file'…
kerwin612 3a6dd02
refactor the PdfViewer
kerwin612 505b644
fix test error
kerwin612 00eb205
fix
kerwin612 3fe3bc6
Merge branch 'main' into feat/support-preview-3d-file
kerwin612 2e7fe6e
clean code
kerwin612 f54fecf
temp
wxiaoguang 2f45268
fix
wxiaoguang 24d7bab
fix
wxiaoguang 9692379
fix test error
kerwin612 9943219
fix css
wxiaoguang d217dd9
refactor
wxiaoguang cade640
fix
wxiaoguang 81bd73c
fix
wxiaoguang eda267d
refactor DetectContentTypeFromReader
wxiaoguang 635c0b8
fix test
wxiaoguang b28f478
fine tune
wxiaoguang 96da512
Merge branch 'main' into feat/support-preview-3d-file
wxiaoguang 340fb0e
fine tune and add more comments
wxiaoguang 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,71 @@ | ||
| /** | ||
| * File View & Render Plugin Styles | ||
| */ | ||
|
|
||
| /* file view container */ | ||
| .file-view-container { | ||
| position: relative; | ||
| width: 100%; | ||
| min-height: 200px; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .file-view-container.is-loading { | ||
| position: relative; | ||
| } | ||
|
|
||
| .file-view-container.is-loading::after { | ||
kerwin612 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| content: ""; | ||
| position: absolute; | ||
| left: 50%; | ||
| top: 50%; | ||
| width: 40px; | ||
| height: 40px; | ||
| margin-left: -20px; | ||
| margin-top: -20px; | ||
| border: 5px solid var(--color-secondary); | ||
| border-top-color: transparent; | ||
| border-radius: 50%; | ||
| animation: spin 1s linear infinite; | ||
| } | ||
|
|
||
| .view-raw-fallback { | ||
| padding: 16px; | ||
| text-align: center; | ||
| } | ||
|
|
||
| /* 3D model viewer */ | ||
| .model3d-content { | ||
| width: 100% !important; | ||
| min-height: 400px !important; | ||
| border: none !important; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| @keyframes spin { | ||
| 0% { | ||
| transform: rotate(0deg); | ||
| } | ||
| 100% { | ||
| transform: rotate(360deg); | ||
| } | ||
| } | ||
|
|
||
| /* error message */ | ||
| .file-view-container .ui.error.message { | ||
| margin: 1em 0; | ||
| width: 100%; | ||
| } | ||
|
|
||
| .file-view-container .ui.error.message pre { | ||
| margin-top: 0.5em; | ||
| font-size: 12px; | ||
| max-height: 150px; | ||
| overflow: auto; | ||
| background-color: rgba(255, 255, 255, 0.1); | ||
| padding: 0.5em; | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -85,4 +85,6 @@ | |
|
|
||
| @import "./helpers.css"; | ||
|
|
||
| @import "./file-view.css"; | ||
|
|
||
| @tailwind utilities; | ||
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,59 @@ | ||
| import {applyRenderPlugin} from '../modules/file-render-plugin.ts'; | ||
| import {registerGlobalInitFunc} from '../modules/observer.ts'; | ||
|
|
||
| /** | ||
| * init file view renderer | ||
| * | ||
| * detect renderable files and apply appropriate plugins | ||
| */ | ||
| export function initFileView(): void { | ||
| // register file view renderer init function | ||
| registerGlobalInitFunc('initFileView', async (container: HTMLElement) => { | ||
| // get file info | ||
| const filename = container.getAttribute('data-filename'); | ||
| const fileUrl = container.getAttribute('data-url'); | ||
|
|
||
| // mark loading state | ||
| container.classList.add('is-loading'); | ||
|
|
||
| try { | ||
| // check if filename and url exist | ||
| if (!filename || !fileUrl) { | ||
| console.error(`missing filename(${filename}) or file url(${fileUrl}) for rendering`); | ||
| throw new Error('missing necessary file info'); | ||
| } | ||
|
|
||
| // try to apply render plugin | ||
| const success = await applyRenderPlugin(container); | ||
|
|
||
| // if no suitable plugin is found, show default view | ||
| if (!success) { | ||
| // show default view raw file link | ||
| const fallbackText = container.getAttribute('data-fallback-text') || 'View Raw File'; | ||
|
|
||
| container.innerHTML = ` | ||
| <div class="view-raw-fallback"> | ||
| <a href="${fileUrl}" class="ui basic button" target="_blank">${fallbackText}</a> | ||
| </div> | ||
| `; | ||
| } | ||
| } catch (error) { | ||
| console.error('file view init error:', error); | ||
|
|
||
| // show error message | ||
| const fallbackText = container.getAttribute('data-fallback-text') || 'View Raw File'; | ||
kerwin612 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| container.innerHTML = ` | ||
| <div class="ui error message"> | ||
| <div class="header">Failed to render file</div> | ||
kerwin612 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <p>Error: ${String(error)}</p> | ||
kerwin612 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <pre>${JSON.stringify({filename, fileUrl}, null, 2)}</pre> | ||
| <a class="ui basic button" href="${fileUrl || '#'}" target="_blank">${fallbackText}</a> | ||
| </div> | ||
| `; | ||
| } finally { | ||
| // remove loading state | ||
| container.classList.remove('is-loading'); | ||
| } | ||
| }); | ||
| } | ||
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,69 @@ | ||
| /** | ||
| * File Render Plugin System | ||
| * | ||
| * This module provides a plugin architecture for rendering different file types | ||
| * in the browser without requiring backend support for identifying file types. | ||
| */ | ||
|
|
||
| /** | ||
| * Interface for file render plugins | ||
| */ | ||
| export type FileRenderPlugin = { | ||
| // unique plugin name | ||
| name: string; | ||
|
|
||
| // test if plugin can handle specified file | ||
| canHandle: (filename: string, mimeType: string) => boolean; | ||
|
|
||
| // render file content | ||
| render: (container: HTMLElement, fileUrl: string, options?: any) => Promise<void>; | ||
| } | ||
|
|
||
| // store registered render plugins | ||
| const plugins: FileRenderPlugin[] = []; | ||
|
|
||
| /** | ||
| * register a file render plugin | ||
| */ | ||
| export function registerFileRenderPlugin(plugin: FileRenderPlugin): void { | ||
| plugins.push(plugin); | ||
| } | ||
|
|
||
| /** | ||
| * find suitable render plugin by filename and mime type | ||
| */ | ||
| function findPlugin(filename: string, mimeType: string): FileRenderPlugin | null { | ||
| return plugins.find((plugin) => plugin.canHandle(filename, mimeType)) || null; | ||
| } | ||
|
|
||
| /** | ||
| * apply render plugin to specified container | ||
| */ | ||
| export async function applyRenderPlugin(container: HTMLElement): Promise<boolean> { | ||
| try { | ||
| // get file info from container element | ||
| const filename = container.getAttribute('data-filename') || ''; | ||
| const fileUrl = container.getAttribute('data-url') || ''; | ||
|
|
||
| if (!filename || !fileUrl) { | ||
| console.warn('Missing filename or file URL for renderer'); | ||
| return false; | ||
| } | ||
|
|
||
| // get mime type (optional) | ||
| const mimeType = container.getAttribute('data-mime-type') || ''; | ||
|
|
||
| // find plugin that can handle this file | ||
| const plugin = findPlugin(filename, mimeType); | ||
| if (!plugin) { | ||
| return false; | ||
| } | ||
|
|
||
| // apply plugin to render file | ||
| await plugin.render(container, fileUrl); | ||
| return true; | ||
| } catch (error) { | ||
| console.error('Error applying render plugin:', error); | ||
| return false; | ||
| } | ||
| } |
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.
IIRC it bloats the binary about 1MB
Can we introduce an config option?
For example:
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.
From a user's perspective, I personally feel that direct integration is better than dynamic loading.
Because building and installation are one-time processes.
However, if we switch to dynamic loading, each time it might require dynamically loading 1MB of JavaScript.
Especially in the current Chinese network environment, sometimes even some external CDNs cannot be accessed, which would directly make the preview unavailable.
So I personally vote for integration.
Of course, the final decision is up to you.
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.
Not "each", only once, there is browser cahce.
That's why a config option to let users choose, or deploy one locally.
If we build the renders into Gitea's binary, then it bloats the binary soon when we introduce more in the future.
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.
I fully understand your original intention, but I think this still stems from the perspective of operators. From a user's point of view, such an option shouldn't exist (or there's simply no need for it at all) – the default provided to users should inherently be the optimal solution.
Of course, I recognize that Gitea, as an open-source platform, does have many "operator" users. Your original intention might be to allow them to choose freely, but personally, I wonder if such customization is getting too caught up in minor details?
Alright, I've shared my thoughts. It's up to you all. If adjustments are needed, I'll cooperate with the changes.
Uh oh!
There was an error while loading. Please reload this page.
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.
Share some of my thoughts:
There are different requirements for various kinds of files to render.
So the question is how to make Gitea have a stable and flexible design to satisfy more renders in the future. Then we have some choices:
I haven't got a clear picture of this feature at the moment. What do other maintainers think about?
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.
No
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.
I think the memory hog is more the git command.
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.
I don't understand what's the point or whether it is related to the topic.
Uh oh!
There was an error while loading. Please reload this page.
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.
That's a separate issue though and there's not much that can be done to limit it. Also it's (IMO) a very large issue for limited memory scenarios which is entirely of topic here.
I can't say I know a ton about this, but here point is that if you bloat the binary it had to be loaded and run each time hook runs which is a waste of resources - hope I got that right.
Honestly hook doesn't even have to have inits(not too sure about it though?) - it just needs to know where gitea is to do RPC.
Sorry for derailing the scope.
Uh oh!
There was an error while loading. Please reload this page.
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.
In history, the "gitea hook" sub commands always do various init calls (including load assets), for example:
var re = regexp.MustCompile,init() { ... }, etcWhen I refactor existing code, usually I also rewrite these code to something like
sync.OnceValueand void unnecessary init.About the binary size affect, it's more complicated and it depends on some OS behaviors like virtual memory/mmap management, usually the binary content should be cached in OS's shared-memory area when it runs, but it's difficult to evaluate all cases (especially when the memory is limited in a container), I am not expert either so it's just a guess.
So I also agree that "if increasing the binary size brings enough benefits, let's do it", and I think "It's fine to make this render builtin".