Skip to content

Conversation

@fly602
Copy link
Contributor

@fly602 fly602 commented Nov 27, 2025

当识别到空文件, 在尝试调用gio的guess方法通过文件名来识别MIME类型,如果还是识别不出来,默认使用text/plain

Log: dde-open增强文件识别
Influence: MIME

Summary by Sourcery

Improve dde-open MIME type detection for empty files and update core linuxdeepin Go dependencies.

Bug Fixes:

  • Ensure empty files are opened by inferring MIME type from filename and defaulting to text/plain when detection is uncertain.

Enhancements:

  • Add debug logging for detected and guessed MIME content types to aid troubleshooting.

Build:

  • Bump linuxdeepin go-dbus-factory, go-gir, and go-lib module versions in go.mod and go.sum.

当识别到空文件, 在尝试调用gio的guess方法通过文件名来识别MIME类型,如果还是识别不出来,默认使用text/plain

Log: dde-open增强文件识别
Influence: MIME
@sourcery-ai
Copy link

sourcery-ai bot commented Nov 27, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Handle empty-file MIME detection in dde-open by falling back to gio-based content type guessing and defaulting to text/plain, and update deepin go dependencies to newer revisions.

Sequence diagram for dde-open empty-file MIME resolution

sequenceDiagram
  participant User
  participant dde_open
  participant gio

  User->>dde_open: openFile(filename)
  dde_open->>dde_open: Detect contentType from file
  alt contentType is empty
    dde_open-->>User: error failed to get file content type
  else contentType == application/x-zerosize
    dde_open->>gio: ContentTypeGuess(filename, nil)
    gio-->>dde_open: uncerten, guessedType
    alt uncerten is true or guessedType empty
      dde_open->>dde_open: Set contentType to text/plain
    else guessedType == application/x-zerosize
      dde_open->>dde_open: Set contentType to text/plain
    else guessedType valid
      dde_open->>dde_open: Set contentType to guessedType
    end
    dde_open->>gio: AppInfoGetDefaultForType(contentType, false)
    gio-->>dde_open: appInfo
    alt appInfo is nil
      dde_open-->>User: error no default app
    else appInfo found
      dde_open->>gio: Launch app with appInfo
      gio-->>User: Application opens file
    end
  else non empty and known contentType
    dde_open->>gio: AppInfoGetDefaultForType(contentType, false)
    gio-->>dde_open: appInfo
    alt appInfo is nil
      dde_open-->>User: error no default app
    else appInfo found
      dde_open->>gio: Launch app with appInfo
      gio-->>User: Application opens file
    end
  end
Loading

Flow diagram for openFile MIME detection with empty file handling

flowchart TD
  A[Start openFile] --> B[Read file and detect contentType]
  B --> C{contentType empty?}
  C -- Yes --> D[Return error failed to get file content type]
  C -- No --> E{contentType == application/x-zerosize?}
  E -- No --> I[Use contentType to get default app via gio.AppInfoGetDefaultForType]
  E -- Yes --> F[Call gio.ContentTypeGuess with filename]
  F --> G{uncerten is true or guessedType empty?}
  G -- Yes --> H[Set contentType to text/plain]
  G -- No --> J{guessedType == application/x-zerosize?}
  J -- Yes --> H
  J -- No --> K[Set contentType to guessedType]
  H --> I
  K --> I
  I --> L{appInfo is nil?}
  L -- Yes --> M[Return error no default app]
  L -- No --> N[Launch app with appInfo]
  N --> O[End openFile]
Loading

File-Level Changes

Change Details Files
Add special handling for empty files so dde-open can determine a usable MIME type instead of failing.
  • Log the initially detected content type before further processing
  • Treat content type "application/x-zerosize" as an empty-file indicator
  • Invoke gio.ContentTypeGuess with the filename when an empty file is detected
  • Fallback to "text/plain" when the guess is uncertain, empty, or still reports "application/x-zerosize"
  • Use the guessed MIME type when it is non-empty and not x-zerosize, and add debug logging for this path
dde-open/main.go
Update deepin-related Go module dependencies to newer commits.
  • Bump github.com/linuxdeepin/go-dbus-factory to a newer pseudo-version
  • Bump github.com/linuxdeepin/go-gir to a newer pseudo-version
  • Bump github.com/linuxdeepin/go-lib to a newer pseudo-version
  • Refresh go.sum to reflect updated module versions
go.mod
go.sum

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link

deepin pr auto review

我来对这个diff进行审查:

  1. 代码功能改进:
  • 添加了对空文件的处理逻辑,这是一个很好的改进。当文件大小为0时,会尝试根据文件扩展名猜测内容类型,这提高了用户体验。
  • 如果无法确定类型或类型仍为空文件,则默认使用文本编辑器打开,这是一个合理的默认行为。
  1. 代码质量建议:
  • 变量名"uncerten"有拼写错误,应该是"uncertain"。
  • 日志输出使用了Debugf级别是合适的,因为这是调试信息。
  • 代码结构清晰,逻辑分支处理完整。
  1. 性能考虑:
  • 新增的空文件检测和类型猜测逻辑只会对空文件执行,不会影响正常文件的性能。
  • gio.ContentTypeGuess() 是一个轻量级操作,性能影响很小。
  1. 安全性:
  • 代码没有引入新的安全风险。
  • 使用了 gio.ContentTypeGuess() 的安全API,没有直接操作文件内容。
  1. 依赖更新:
  • go.mod 和 go.sum 中更新了几个 deepin 相关的依赖包版本,这是常规的依赖更新。

改进建议:

  1. 修正拼写错误:
uncertain, guessedType := gio.ContentTypeGuess(filename, nil)
  1. 可以考虑添加更多日志信息,比如在无法确定类型时也记录日志:
if uncertain || guessedType == "" {
    logger.Debug("Failed to guess content type for empty file, using text/plain")
    contentType = "text/plain"
}
  1. 可以考虑将默认的 content type "text/plain" 定义为常量,便于维护:
const defaultContentType = "text/plain"

总体来说,这是一个很好的改进,提高了对空文件的处理能力,代码质量良好,没有明显的问题。

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The variable name uncerten is likely a typo for uncertain; consider renaming it to improve readability and avoid confusion.
  • The conditional branches handling guessedType repeat the application/x-zerosize check and empty-string case; you could simplify by treating both as a single fallback path to text/plain to make the logic clearer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The variable name `uncerten` is likely a typo for `uncertain`; consider renaming it to improve readability and avoid confusion.
- The conditional branches handling `guessedType` repeat the `application/x-zerosize` check and empty-string case; you could simplify by treating both as a single fallback path to `text/plain` to make the logic clearer.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, fly602

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fly602 fly602 merged commit ff328ab into linuxdeepin:master Nov 27, 2025
15 of 17 checks passed
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.

3 participants