Skip to content

Conversation

@pengfeixx
Copy link
Contributor

Fix custom avatar issue

Log: Fix custom avatar issue
pms: BUG-298689

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.

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-bot
Copy link

deepin-bot bot commented Oct 10, 2025

TAG Bot

New tag: 6.1.51
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #2724

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, pengfeixx

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

Fix custom avatar issue

Log: Fix custom avatar issue
pms: BUG-298689
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查报告

总体评价

这次代码更新主要涉及账户系统中的头像功能改进,包括头像的存储、管理、裁剪和显示等方面。代码质量总体良好,但存在一些可以改进的地方。

具体问题与建议

1. 代码逻辑问题

问题AccountsController::avatars() 函数中,对 "icons/local" 过滤器的处理逻辑发生了变化,从函数末尾移到了函数开始。

建议

  • 这种改动可能导致代码逻辑不清晰,建议保持原有结构,或者将相关逻辑封装为独立函数
  • 对于过滤器的处理,建议使用策略模式或工厂模式来处理不同类型的过滤器

2. 性能问题

问题:在 AccountsWorker::saveCustomAvatar() 中,频繁的文件系统操作可能影响性能。

建议

// 在文件操作前添加缓存检查
if (QFileInfo(targetFile).exists() && QFileInfo(tempAbs).size() == QFileInfo(targetFile).size()) {
    return QUrl::fromLocalFile(targetFile).toString();
}

问题CustomLocalAvatarsRow.qml 中的 refresh() 函数每次都重新获取所有头像列表。

建议

// 添加缓存机制
property var avatarCache: []
property var lastRefreshTime: 0

function shouldRefresh() {
    return Date.now() - lastRefreshTime > 5000 // 5秒缓存
}

function refresh() {
    if (!shouldRefresh()) return
    lastRefreshTime = Date.now()
    // ... 原有刷新逻辑
}

3. 安全问题

问题:文件路径处理存在潜在的安全风险。

建议

// 在 AccountsWorker::saveCustomAvatar() 中添加路径验证
bool isSafePath(const QString &path) {
    QDir dir(path);
    return dir.isAbsolutePath() && 
           dir.absolutePath().startsWith(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
}

if (!isSafePath(targetFile)) {
    qWarning() << "Unsafe file path detected:" << targetFile;
    return QString();
}

问题:在 CustomAvatarCropper.qml 中,临时文件的处理不够安全。

建议

// 添加文件权限检查
function cropToTemp(callback) {
    if (!control.iconSource || control.iconSource.length < 1) {
        if (callback) callback("")
        return
    }
    
    const tmp = StandardPaths.writableLocation(StandardPaths.TempLocation) + "/dcc_avatar_tmp.png"
    imgGrabber.grabToImage(function (result) {
        result.saveToFile(tmp)
        // 设置适当的文件权限
        Qt.callLater(function() {
            const file = new File(tmp)
            file.setPermissions(File.ReadOwner | File.WriteOwner)
            if (callback) callback(tmp)
        })
    })
}

4. 代码质量问题

问题:一些函数过长,职责不够单一。

建议

  • AccountsWorker::setAvatar() 中的缓存相关逻辑提取为独立函数
  • AvatarSettingsDialog.qml 中的保存逻辑封装为独立组件

问题:部分代码存在重复。

建议

// 提取公共的路径处理函数
namespace AvatarPaths {
    QString getAvatarsDir() {
        const QString appCacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
        return appCacheDir + QDir::separator() + "avatars";
    }
    
    QString getMarkerFile() {
        return getAvatarsDir() + QDir::separator() + "current";
    }
}

5. 用户体验问题

问题:头像裁剪组件的交互不够直观。

建议

  • 添加缩放限制,防止图片过大或过小
  • 添加裁剪区域预览,让用户更清楚裁剪范围
  • 添加重置按钮,方便用户重新调整
// 在 CustomAvatarCropper.qml 中添加
Button {
    text: qsTr("Reset")
    onClicked: {
        img2.scale = 1.0
        img2.updatePos()
    }
    anchors.bottom: parent.bottom
    anchors.horizontalCenter: parent.horizontalCenter
}

总结

这次更新改进了头像功能的多个方面,但仍有一些可以优化的地方。建议重点关注性能优化、安全性增强和代码重构,同时改进用户体验。对于已经实现的缓存机制和文件管理功能,可以进一步扩展和完善。

@pengfeixx
Copy link
Contributor Author

/merge

@deepin-bot deepin-bot bot merged commit f122fdc into linuxdeepin:master Oct 13, 2025
17 checks passed
@pengfeixx pengfeixx deleted the fix-298689 branch October 13, 2025 01:12
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