2024-07-23 14:48:37 +08:00
package service
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
2024-08-13 15:33:34 +08:00
"net/http"
"os"
2025-10-15 17:37:07 +08:00
"path"
2024-08-13 15:33:34 +08:00
"path/filepath"
"reflect"
"strconv"
"strings"
2026-02-05 13:44:59 +08:00
"sync"
2026-04-15 15:44:20 +08:00
"time"
2024-08-13 15:33:34 +08:00
2025-03-24 17:37:31 +08:00
"github.com/gin-gonic/gin"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
2024-07-29 18:09:57 +08:00
"github.com/1Panel-dev/1Panel/agent/app/task"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/i18n"
2026-01-27 14:02:38 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/appicon"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/common"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/1Panel-dev/1Panel/agent/utils/files"
2025-01-22 14:28:22 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/req_helper"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
"gopkg.in/yaml.v3"
)
2026-02-05 13:44:59 +08:00
var (
appStoreSyncMu sync . Mutex
appStoreSyncing bool
)
2024-07-23 14:48:37 +08:00
type AppService struct {
}
type IAppService interface {
2025-11-20 18:57:49 +08:00
PageApp ( ctx * gin . Context , req request . AppSearch ) ( * response . AppRes , error )
2025-02-06 18:15:06 +08:00
GetAppTags ( ctx * gin . Context ) ([] response . TagDTO , error )
GetApp ( ctx * gin . Context , key string ) ( * response . AppDTO , error )
2024-07-23 14:48:37 +08:00
GetAppDetail ( appId uint , version , appType string ) ( response . AppDetailDTO , error )
2026-02-05 16:54:55 +08:00
Install ( req request . AppInstallCreate , executeScript bool ) ( * model . AppInstall , error )
2024-08-22 18:48:55 +08:00
SyncAppListFromRemote ( taskID string ) error
2024-07-23 14:48:37 +08:00
GetAppUpdate () ( * response . AppUpdateRes , error )
GetAppDetailByID ( id uint ) ( * response . AppDetailDTO , error )
2024-08-23 17:22:05 +08:00
SyncAppListFromLocal ( taskID string )
2026-01-27 14:02:38 +08:00
GetAppIcon ( key string ) ([] byte , string , string , error )
2026-01-04 13:43:40 +08:00
GetAppDetailByKey ( appKey , version string ) ( response . AppDetailSimpleDTO , error )
2024-07-23 14:48:37 +08:00
}
2026-11-06 00:44:50 +08:00
type AppInstallHooks struct {
AfterCreate func ( appInstall * model . AppInstall ) error
2026-10-28 14:04:38 +08:00
AfterCopyData func ( appInstall * model . AppInstall ) error
2026-11-25 22:25:08 +08:00
AfterStart func ( * model . AppInstall , * task . Task ) error
AfterFinish func ()
2026-10-28 14:04:38 +08:00
PrepareSiteDir func ( model . App , model . AppDetail , request . AppInstallCreate , * task . Task ) error
2026-03-19 17:00:15 +08:00
}
2024-07-23 14:48:37 +08:00
func NewIAppService () IAppService {
return & AppService {}
}
2025-11-20 18:57:49 +08:00
func ( a AppService ) PageApp ( ctx * gin . Context , req request . AppSearch ) ( * response . AppRes , error ) {
2026-11-24 09:58:06 +08:00
opts := [] repo . DBOption { appRepo . OrderByRecommend (), appRepo . WithKeyNotIn ( constant . ClusterAppKeys ())}
2024-07-23 14:48:37 +08:00
if req . Name != "" {
2025-08-22 11:07:51 +08:00
opts = append ( opts , appRepo . WithByLikeName ( strings . TrimSpace ( req . Name )))
2024-07-23 14:48:37 +08:00
}
if req . Type != "" {
opts = append ( opts , appRepo . WithType ( req . Type ))
}
if req . Recommend {
opts = append ( opts , appRepo . GetRecommend ())
}
if req . Resource != "" && req . Resource != "all" {
opts = append ( opts , appRepo . WithResource ( req . Resource ))
}
2024-12-23 22:05:44 +08:00
2024-08-22 15:08:55 +08:00
if req . ShowCurrentArch {
info , err := NewIDashboardService (). LoadOsInfo ()
if err != nil {
return nil , err
}
2025-10-13 21:28:53 +08:00
kernelArch := info . KernelArch
if kernelArch == "aarch64" {
kernelArch = "arm64"
}
opts = append ( opts , appRepo . WithArch ( kernelArch ))
2024-08-22 15:08:55 +08:00
}
2024-07-23 14:48:37 +08:00
if len ( req . Tags ) != 0 {
tags , err := tagRepo . GetByKeys ( req . Tags )
if err != nil {
return nil , err
}
var tagIds [] uint
for _ , t := range tags {
tagIds = append ( tagIds , t . ID )
}
appTags , err := appTagRepo . GetByTagIds ( tagIds )
if err != nil {
return nil , err
}
var appIds [] uint
for _ , t := range appTags {
appIds = append ( appIds , t . AppId )
}
2025-01-02 17:20:03 +08:00
opts = append ( opts , repo . WithByIDs ( appIds ))
2024-07-23 14:48:37 +08:00
}
2025-11-20 18:57:49 +08:00
res := & response . AppRes {}
2024-12-23 22:05:44 +08:00
2024-07-23 14:48:37 +08:00
total , apps , err := appRepo . Page ( req . Page , req . PageSize , opts ... )
if err != nil {
return nil , err
}
2025-08-22 11:07:51 +08:00
appDTOs := make ([] * response . AppItem , 0 )
2024-12-23 22:05:44 +08:00
info := & dto . SettingInfo {}
if req . Type == "php" {
info , _ = NewISettingService (). GetSettingInfo ()
}
2025-02-06 18:15:06 +08:00
lang := strings . ToLower ( common . GetLang ( ctx ))
2024-07-23 14:48:37 +08:00
for _ , ap := range apps {
2024-12-23 22:05:44 +08:00
if req . Type == "php" {
2026-04-30 11:05:55 +08:00
if ! global . CONF . Base . IsOffline && ( ap . RequiredPanelVersion == 0 || ! common . CompareAppVersion ( common . GetSystemVersion ( info . SystemVersion ), fmt . Sprintf ( "%f" , ap . RequiredPanelVersion ))) {
2024-12-23 22:05:44 +08:00
continue
}
}
2025-02-06 18:15:06 +08:00
appDTO := & response . AppItem {
2026-01-04 13:43:40 +08:00
ID : ap . ID ,
Name : ap . Name ,
Key : ap . Key ,
2026-10-30 18:14:41 +08:00
Icon : appicon . RemoteURL ( ap . Icon ),
2026-01-04 13:43:40 +08:00
Limit : ap . Limit ,
GpuSupport : ap . GpuSupport ,
Recommend : ap . Recommend ,
Description : ap . GetDescription ( ctx ),
Type : ap . Type ,
BatchInstallSupport : ap . BatchInstallSupport ,
2024-07-23 14:48:37 +08:00
}
appDTOs = append ( appDTOs , appDTO )
2025-02-06 18:15:06 +08:00
tags , err := getAppTags ( ap . ID , lang )
2024-07-23 14:48:37 +08:00
if err != nil {
continue
}
2025-11-20 18:57:49 +08:00
for _ , tag := range tags {
appDTO . Tags = append ( appDTO . Tags , tag . Name )
}
2025-08-28 15:56:08 +08:00
if ap . Type == constant . RuntimePHP || ap . Type == constant . RuntimeGo || ap . Type == constant . RuntimeNode || ap . Type == constant . RuntimePython || ap . Type == constant . RuntimeJava || ap . Type == constant . RuntimeDotNet {
details , _ := appDetailRepo . GetBy ( appDetailRepo . WithAppId ( ap . ID ))
var ids [] uint
if len ( details ) == 0 {
continue
}
for _ , d := range details {
ids = append ( ids , d . ID )
}
runtimes , _ := runtimeRepo . List ( runtimeRepo . WithDetailIdsIn ( ids ))
appDTO . Installed = len ( runtimes ) > 0
} else {
installs , _ := appInstallRepo . ListBy ( context . Background (), appInstallRepo . WithAppId ( ap . ID ))
appDTO . Installed = len ( installs ) > 0
}
2024-07-23 14:48:37 +08:00
}
res . Items = appDTOs
res . Total = total
return res , nil
}
2025-02-06 18:15:06 +08:00
func ( a AppService ) GetAppTags ( ctx * gin . Context ) ([] response . TagDTO , error ) {
2024-07-23 14:48:37 +08:00
tags , err := tagRepo . All ()
if err != nil {
return nil , err
}
var res [] response . TagDTO
2025-02-06 18:15:06 +08:00
lang := strings . ToLower ( common . GetLang ( ctx ))
2024-07-23 14:48:37 +08:00
for _ , tag := range tags {
2025-02-06 18:15:06 +08:00
tagDTO := response . TagDTO {
ID : tag . ID ,
Key : tag . Key ,
}
var translations = make ( map [ string ] string )
_ = json . Unmarshal ([] byte ( tag . Translations ), & translations )
if name , ok := translations [ lang ]; ok {
tagDTO . Name = name
}
res = append ( res , tagDTO )
2024-07-23 14:48:37 +08:00
}
return res , nil
}
2025-02-06 18:15:06 +08:00
func ( a AppService ) GetApp ( ctx * gin . Context , key string ) ( * response . AppDTO , error ) {
2024-07-23 14:48:37 +08:00
var appDTO response . AppDTO
2024-11-30 19:42:10 +08:00
if key == "postgres" {
key = "postgresql"
}
2024-07-23 14:48:37 +08:00
app , err := appRepo . GetFirst ( appRepo . WithKey ( key ))
if err != nil {
return nil , err
}
appDTO . App = app
2026-11-24 09:58:06 +08:00
appDTO . ClusterOnly = constant . IsClusterApp ( app . Key )
2025-02-06 18:15:06 +08:00
appDTO . App . Description = app . GetDescription ( ctx )
2024-07-23 14:48:37 +08:00
details , err := appDetailRepo . GetBy ( appDetailRepo . WithAppId ( app . ID ))
if err != nil {
return nil , err
}
2025-12-03 11:56:15 +08:00
appDTO . Versions = getAppVersions ( key , details )
2025-02-06 18:15:06 +08:00
tags , err := getAppTags ( app . ID , strings . ToLower ( common . GetLang ( ctx )))
if err != nil {
return nil , err
}
appDTO . Tags = tags
2024-07-23 14:48:37 +08:00
return & appDTO , nil
}
2026-01-04 13:43:40 +08:00
func ( a AppService ) GetAppDetailByKey ( appKey , version string ) ( response . AppDetailSimpleDTO , error ) {
var appDetailDTO response . AppDetailSimpleDTO
app , err := appRepo . GetFirst ( appRepo . WithKey ( appKey ))
if err != nil {
return appDetailDTO , err
}
2026-09-07 14:56:56 +08:00
if err = checkVllmVersionAccess ( app . Key , version ); err != nil {
return appDetailDTO , err
}
2026-01-04 13:43:40 +08:00
appDetail , err := appDetailRepo . GetFirst ( appDetailRepo . WithAppId ( app . ID ), appDetailRepo . WithVersion ( version ))
if err != nil {
return appDetailDTO , err
}
appDetailDTO . ID = appDetail . ID
return appDetailDTO , nil
}
2024-07-23 14:48:37 +08:00
func ( a AppService ) GetAppDetail ( appID uint , version , appType string ) ( response . AppDetailDTO , error ) {
var (
appDetailDTO response . AppDetailDTO
opts [] repo . DBOption
)
opts = append ( opts , appDetailRepo . WithAppId ( appID ), appDetailRepo . WithVersion ( version ))
detail , err := appDetailRepo . GetFirst ( opts ... )
if err != nil {
return appDetailDTO , err
}
2026-09-07 14:56:56 +08:00
app , err := appRepo . GetFirst ( repo . WithByID ( detail . AppId ))
if err != nil {
return appDetailDTO , err
}
if err = checkVllmVersionAccess ( app . Key , detail . Version ); err != nil {
return appDetailDTO , err
}
2024-07-23 14:48:37 +08:00
appDetailDTO . AppDetail = detail
appDetailDTO . Enable = true
if appType == "runtime" {
fileOp := files . NewFileOp ()
versionPath := filepath . Join ( app . GetAppResourcePath (), detail . Version )
2026-10-22 11:53:36 +08:00
if err = downloadApp ( app , detail , nil , nil ); err != nil && ! fileOp . Stat ( versionPath ) {
return appDetailDTO , err
}
2026-10-30 18:14:41 +08:00
packageReady := err == nil
if packageReady && app . Resource == constant . AppResourceRemote {
2026-10-22 11:53:36 +08:00
detail . Update = false
appDetailDTO . Update = false
2024-07-23 14:48:37 +08:00
}
2026-10-30 18:14:41 +08:00
if packageReady && strings . TrimSpace ( detail . DockerCompose ) == "" {
compose , err := fileOp . GetContent ( filepath . Join ( versionPath , "docker-compose.yml" ))
if err != nil {
return appDetailDTO , err
}
if err := cacheAppDetailCompose ( & detail , compose ); err != nil {
return appDetailDTO , err
}
}
2024-07-23 14:48:37 +08:00
switch app . Type {
case constant . RuntimePHP :
paramMap := make ( map [ string ] interface {})
2026-10-22 11:53:36 +08:00
if app . Resource == constant . AppResourceRemote {
if err = json . Unmarshal ([] byte ( detail . Params ), & paramMap ); err != nil {
return appDetailDTO , err
}
appDetailDTO . Params = paramMap
} else {
paramsPath := filepath . Join ( versionPath , "data.yml" )
if ! fileOp . Stat ( paramsPath ) {
return appDetailDTO , buserr . WithDetail ( "ErrFileNotExist" , paramsPath , nil )
}
param , err := fileOp . GetContent ( paramsPath )
if err != nil {
return appDetailDTO , err
}
if err = yaml . Unmarshal ( param , & paramMap ); err != nil {
return appDetailDTO , err
}
appDetailDTO . Params = paramMap [ "additionalProperties" ]
2024-07-23 14:48:37 +08:00
}
2024-09-02 21:56:24 +08:00
composePath := filepath . Join ( versionPath , "docker-compose.yml" )
2024-07-23 14:48:37 +08:00
if ! fileOp . Stat ( composePath ) {
2025-01-24 11:00:07 +08:00
return appDetailDTO , buserr . WithDetail ( "ErrFileNotExist" , composePath , nil )
2024-07-23 14:48:37 +08:00
}
compose , err := fileOp . GetContent ( composePath )
if err != nil {
return appDetailDTO , err
}
2026-10-30 18:14:41 +08:00
if err := validateAppDetailCompose ( compose ); err != nil {
return appDetailDTO , err
}
var composeMap struct {
Services map [ string ] interface {} `yaml:"services"`
}
2024-07-23 14:48:37 +08:00
if err := yaml . Unmarshal ( compose , & composeMap ); err != nil {
return appDetailDTO , err
}
2026-10-30 18:14:41 +08:00
for k := range composeMap . Services {
appDetailDTO . Image = k
2024-07-23 14:48:37 +08:00
}
}
} else {
paramMap := make ( map [ string ] interface {})
if err := json . Unmarshal ([] byte ( detail . Params ), & paramMap ); err != nil {
return appDetailDTO , err
}
appDetailDTO . Params = paramMap
}
2026-10-30 18:14:41 +08:00
if err := ensureAppDetailCompose ( app , & detail ); err != nil {
return appDetailDTO , err
2024-07-23 14:48:37 +08:00
}
2026-10-30 18:14:41 +08:00
appDetailDTO . DockerCompose = detail . DockerCompose
2024-07-23 14:48:37 +08:00
appDetailDTO . HostMode = isHostModel ( appDetailDTO . DockerCompose )
if err := checkLimit ( app ); err != nil {
appDetailDTO . Enable = false
}
2024-08-22 15:08:55 +08:00
appDetailDTO . Architectures = app . Architectures
2024-08-26 13:40:27 +08:00
appDetailDTO . MemoryRequired = app . MemoryRequired
appDetailDTO . GpuSupport = app . GpuSupport
2024-07-23 14:48:37 +08:00
return appDetailDTO , nil
}
func ( a AppService ) GetAppDetailByID ( id uint ) ( * response . AppDetailDTO , error ) {
res := & response . AppDetailDTO {}
2025-01-02 17:20:03 +08:00
appDetail , err := appDetailRepo . GetFirst ( repo . WithByID ( id ))
2024-07-23 14:48:37 +08:00
if err != nil {
return nil , err
}
2026-10-30 18:14:41 +08:00
app , err := appRepo . GetFirst ( repo . WithByID ( appDetail . AppId ))
if err != nil {
return nil , err
}
if err := ensureAppDetailCompose ( app , & appDetail ); err != nil {
return nil , err
}
2024-07-23 14:48:37 +08:00
res . AppDetail = appDetail
paramMap := make ( map [ string ] interface {})
if err := json . Unmarshal ([] byte ( appDetail . Params ), & paramMap ); err != nil {
return nil , err
}
res . Params = paramMap
res . HostMode = isHostModel ( appDetail . DockerCompose )
return res , nil
}
2026-02-05 16:54:55 +08:00
func ( a AppService ) Install ( req request . AppInstallCreate , executeScript bool ) ( appInstall * model . AppInstall , err error ) {
2026-11-06 00:44:50 +08:00
return a . InstallWithHooks ( req , executeScript , nil )
2026-03-19 17:00:15 +08:00
}
2026-11-24 09:58:06 +08:00
func ( a AppService ) InstallWithHooks ( req request . AppInstallCreate , executeScript bool , hooks * AppInstallHooks ) ( * model . AppInstall , error ) {
return a . installWithHooks ( req , executeScript , hooks , false )
}
func ( a AppService ) InstallCluster ( req request . AppInstallCreate , executeScript bool ) ( * model . AppInstall , error ) {
return a . installWithHooks ( req , executeScript , nil , true )
}
func ( a AppService ) installWithHooks ( req request . AppInstallCreate , executeScript bool , hooks * AppInstallHooks , cluster bool ) ( appInstall * model . AppInstall , err error ) {
2024-07-23 14:48:37 +08:00
var (
httpPort int
httpsPort int
appDetail model . AppDetail
app model . App
)
2025-01-02 17:20:03 +08:00
appDetail , err = appDetailRepo . GetFirst ( repo . WithByID ( req . AppDetailId ))
2024-07-23 14:48:37 +08:00
if err != nil {
return
}
2025-01-02 17:20:03 +08:00
app , err = appRepo . GetFirst ( repo . WithByID ( appDetail . AppId ))
2024-07-23 14:48:37 +08:00
if err != nil {
return
}
2026-11-24 09:58:06 +08:00
if err = validateClusterAppInstall ( app . Key , appDetail . Version , cluster ); err != nil {
return
}
if err = docker . CreateDefaultDockerNetwork (); err != nil {
err = buserr . WithDetail ( "Err1PanelNetworkFailed" , err . Error (), nil )
return
}
if list , _ := appInstallRepo . ListBy ( context . Background (), repo . WithByLowerName ( req . Name )); len ( list ) > 0 {
err = buserr . New ( "ErrAppNameExist" )
return
}
2026-09-07 14:56:56 +08:00
if err = checkVllmVersionAccess ( app . Key , appDetail . Version ); err != nil {
return
}
2024-07-23 14:48:37 +08:00
if DatabaseKeys [ app . Key ] > 0 {
2025-01-02 17:20:03 +08:00
if existDatabases , _ := databaseRepo . GetList ( repo . WithByName ( req . Name )); len ( existDatabases ) > 0 {
2025-01-24 11:00:07 +08:00
err = buserr . New ( "ErrRemoteExist" )
2024-07-23 14:48:37 +08:00
return
}
}
2025-03-04 15:18:04 +08:00
if hostName , ok := req . Params [ "PANEL_DB_HOST" ]; ok {
database , _ := databaseRepo . Get ( repo . WithByName ( hostName .( string )))
if database . AppInstallID > 0 {
databaseInstall , _ := appInstallRepo . GetFirst ( repo . WithByID ( database . AppInstallID ))
if databaseInstall . Status != constant . StatusRunning {
return nil , buserr . WithName ( "ErrAppIsDown" , databaseInstall . Name )
}
}
}
2026-11-25 17:45:02 +08:00
if err = resolveRedisParams ( req . Params , appDetail . Params , false ); err != nil {
return nil , err
}
2024-07-23 14:48:37 +08:00
for key := range req . Params {
if ! strings . Contains ( key , "PANEL_APP_PORT" ) {
continue
}
var port int
2025-01-24 15:59:13 +08:00
port , err = checkPort ( key , req . Params )
if err != nil {
2024-07-23 14:48:37 +08:00
return
}
2025-01-24 15:59:13 +08:00
if key == "PANEL_APP_PORT_HTTP" {
httpPort = port
}
if key == "PANEL_APP_PORT_HTTPS" {
httpsPort = port
}
2024-07-23 14:48:37 +08:00
}
if err = checkRequiredAndLimit ( app ); err != nil {
return
}
appInstall = & model . AppInstall {
Name : req . Name ,
AppId : appDetail . AppId ,
AppDetailId : appDetail . ID ,
Version : appDetail . Version ,
2025-03-04 15:18:04 +08:00
Status : constant . StatusInstalling ,
2024-07-23 14:48:37 +08:00
HttpPort : httpPort ,
HttpsPort : httpsPort ,
App : app ,
}
composeMap := make ( map [ string ] interface {})
2026-11-25 17:45:02 +08:00
composeContent := req . DockerCompose
if ! req . EditCompose {
2026-10-30 18:14:41 +08:00
if err = ensureAppDetailCompose ( app , & appDetail ); err != nil {
return
2026-01-04 13:43:40 +08:00
}
2026-11-25 17:45:02 +08:00
composeContent = appDetail . DockerCompose
2024-07-23 14:48:37 +08:00
}
2026-11-25 17:45:02 +08:00
if err = yaml . Unmarshal ([] byte ( composeContent ), & composeMap ); err != nil {
2024-07-23 14:48:37 +08:00
return
}
2026-11-25 17:45:02 +08:00
servicesMap , ok := composeMap [ "services" ].( map [ string ] interface {})
if ! ok || len ( servicesMap ) == 0 {
return nil , buserr . New ( "ErrFileParse" )
}
2024-07-23 14:48:37 +08:00
containerName := constant . ContainerPrefix + app . Key + "-" + common . RandStr ( 4 )
if req . Advanced && req . ContainerName != "" {
containerName = req . ContainerName
2025-03-12 17:44:03 +08:00
appInstalls , _ := appInstallRepo . ListBy ( context . Background (), appInstallRepo . WithContainerName ( containerName ))
2024-07-23 14:48:37 +08:00
if len ( appInstalls ) > 0 {
2025-01-24 11:00:07 +08:00
err = buserr . New ( "ErrContainerName" )
2024-07-23 14:48:37 +08:00
return
}
containerExist := false
containerExist , err = checkContainerNameIsExist ( req . ContainerName , appInstall . GetPath ())
if err != nil {
return
}
if containerExist {
2025-01-24 11:00:07 +08:00
err = buserr . New ( "ErrContainerName" )
2024-07-23 14:48:37 +08:00
return
}
}
req . Params [ constant . ContainerName ] = containerName
appInstall . ContainerName = containerName
2026-10-23 18:39:22 +08:00
modelAccountBaseURL := ""
if app . Key == "ollama" {
2026-10-26 14:03:40 +08:00
appInstall . SyncModelAccount = req . SyncModelAccount == nil || * req . SyncModelAccount
appInstall . ModelAccountBaseURLType = req . ModelAccountBaseURLType
if appInstall . ModelAccountBaseURLType == "" {
appInstall . ModelAccountBaseURLType = "container"
}
appInstall . ModelAccountBaseURL = req . ModelAccountBaseURL
}
if app . Key == "ollama" && appInstall . SyncModelAccount {
2026-10-23 18:39:22 +08:00
modelAccountBaseURL , err = ResolveLocalModelAccountBaseURL ( req . ModelAccountBaseURLType , req . ModelAccountBaseURL , containerName , "11434" , httpPort )
if err != nil {
return
}
}
2026-11-25 17:45:02 +08:00
serviceName := selectPrimaryComposeService ( servicesMap , composeContent , app . Key )
2025-11-17 11:44:32 +08:00
newServiceName := strings . ToLower ( appInstall . Name )
2026-08-27 18:36:37 +08:00
if app . Limit == 0 && newServiceName != serviceName && len ( servicesMap ) == 1 && ! req . KeepServiceName {
2025-11-17 11:44:32 +08:00
servicesMap [ newServiceName ] = servicesMap [ serviceName ]
2024-07-23 14:48:37 +08:00
delete ( servicesMap , serviceName )
2025-11-17 11:44:32 +08:00
serviceName = newServiceName
2024-07-23 14:48:37 +08:00
}
appInstall . ServiceName = serviceName
2026-08-27 18:36:37 +08:00
if ! req . SkipComposeCommonConfig {
2026-11-08 17:24:01 +08:00
if err = AddDockerComposeCommonParam ( composeMap , appInstall . ServiceName , req . AppContainerConfig , req . Params , app . Key ); err != nil {
2026-08-27 18:36:37 +08:00
return
}
2024-07-23 14:48:37 +08:00
}
var (
composeByte [] byte
paramByte [] byte
)
2026-10-30 17:03:21 +08:00
if ( app . Key == "ollama" || app . Key == "vllm" ) && req . GPU != nil {
gpuServiceName := appInstall . ServiceName
imageType := "nvidia"
if app . Key == "vllm" {
imageType = req . GPUImageType
if len ( servicesMap ) > 1 {
gpuServiceName = "vllm"
if _ , ok := servicesMap [ gpuServiceName ]; ! ok {
gpuServiceName = "vllm-dspark"
}
}
}
service , ok := servicesMap [ gpuServiceName ].( map [ string ] interface {})
if ! ok {
return nil , buserr . New ( "ErrFileParse" )
}
if err = ApplyModelGPU ( service , req . GPU , req . Params , imageType ); err != nil {
return
}
}
2024-07-23 14:48:37 +08:00
composeByte , err = yaml . Marshal ( composeMap )
if err != nil {
return
}
appInstall . DockerCompose = string ( composeByte )
if hostName , ok := req . Params [ "PANEL_DB_HOST" ]; ok {
2025-01-02 17:20:03 +08:00
database , _ := databaseRepo . Get ( repo . WithByName ( hostName .( string )))
2024-07-23 14:48:37 +08:00
if ! reflect . DeepEqual ( database , model . Database {}) {
req . Params [ "PANEL_DB_HOST" ] = database . Address
req . Params [ "PANEL_DB_PORT" ] = database . Port
req . Params [ "PANEL_DB_HOST_NAME" ] = hostName
2025-07-22 11:20:05 +08:00
req . Params [ "DATABASE_NAME" ] = database . Name
2024-07-23 14:48:37 +08:00
}
}
2025-08-25 18:56:58 +08:00
if app . Key == "openresty" {
req . Params [ "CONTAINER_PACKAGE_URL" ] = "http://archive.ubuntu.com/ubuntu/"
req . Params [ "RESTY_ADD_PACKAGE_BUILDDEPS" ] = ""
req . Params [ "RESTY_CONFIG_OPTIONS_MORE" ] = ""
}
2025-10-15 17:37:07 +08:00
if app . Key == "openresty" && ( app . Resource == "remote" || app . Resource == "custom" ) && common . CompareVersion ( appDetail . Version , "1.27" ) {
if dir , ok := req . Params [ "WEBSITE_DIR" ]; ok {
siteDir := dir .( string )
if siteDir == "" || ! strings . HasPrefix ( siteDir , "/" ) {
siteDir = path . Join ( global . Dir . DataDir , dir .( string ))
}
req . Params [ "WEBSITE_DIR" ] = siteDir
}
}
2024-07-23 14:48:37 +08:00
paramByte , err = json . Marshal ( req . Params )
if err != nil {
return
}
appInstall . Env = string ( paramByte )
2026-03-05 15:43:18 +08:00
var maxSort int
global . DB . Model ( & model . AppInstall {}). Where ( "favorite = ?" , false ). Select ( "COALESCE(MAX(sort_order),0)" ). Scan ( & maxSort )
appInstall . SortOrder = maxSort + 1
2024-07-29 18:09:57 +08:00
if err = appInstallRepo . Create ( context . Background (), appInstall ); err != nil {
2024-07-23 14:48:37 +08:00
return
}
2024-07-29 18:09:57 +08:00
2026-11-06 00:44:50 +08:00
if hooks != nil && hooks . AfterCreate != nil {
if err = hooks . AfterCreate ( appInstall ); err != nil {
return
}
}
if app . Key == "ollama" && appInstall . SyncModelAccount {
if _ , err = SyncLocalModelAccount ( * appInstall , 0 , modelAccountBaseURL , nil , false , true , nil ); err != nil {
appInstall . Status = constant . StatusInstallErr
appInstall . Message = err . Error ()
2026-11-13 18:20:13 +08:00
_ = appInstallRepo . UpdateStatus ( appInstall , false )
2026-11-06 00:44:50 +08:00
return
}
}
2024-08-02 16:00:15 +08:00
installTask , err := task . NewTaskWithOps ( appInstall . Name , task . TaskInstall , task . TaskScopeApp , req . TaskID , appInstall . ID )
2024-07-29 18:09:57 +08:00
if err != nil {
2026-10-10 11:13:17 +08:00
appInstall . Status = constant . StatusInstallErr
appInstall . Message = err . Error ()
2026-11-13 18:20:13 +08:00
if saveErr := appInstallRepo . UpdateStatus ( appInstall , false ); saveErr != nil {
2026-10-10 11:13:17 +08:00
err = fmt . Errorf ( "%w; save failed install status: %v" , err , saveErr )
}
2024-07-23 14:48:37 +08:00
return
}
2024-07-29 18:09:57 +08:00
if err = createLink ( context . Background (), installTask , app , appInstall , req . Params ); err != nil {
return
}
2026-10-10 11:13:17 +08:00
installApp := func ( t * task . Task ) ( err error ) {
2024-07-29 18:09:57 +08:00
if err = copyData ( t , app , appDetail , appInstall , req ); err != nil {
return err
}
2026-03-19 17:00:15 +08:00
if hooks != nil && hooks . AfterCopyData != nil {
if err = hooks . AfterCopyData ( appInstall ); err != nil {
return err
}
}
2026-08-27 18:36:37 +08:00
if executeScript || req . UseLifecycleScripts {
2026-02-05 16:54:55 +08:00
if err = runScript ( t , appInstall , "init" ); err != nil {
return err
}
2024-07-29 18:09:57 +08:00
}
2025-03-11 13:45:50 +08:00
if app . Key == "openresty" {
2026-10-28 14:04:38 +08:00
if err = prepareAppInstallSiteDirectory ( app , appDetail , req , t , hooks ); err != nil {
2025-08-29 13:48:20 +08:00
return err
}
2025-03-11 13:45:50 +08:00
if err = handleOpenrestyFile ( appInstall ); err != nil {
return err
}
}
2026-08-27 18:36:37 +08:00
if err = upApp ( t , appInstall , req . PullImage , req . UseLifecycleScripts ); err != nil {
2024-12-23 16:59:46 +08:00
return err
}
2026-11-25 22:25:08 +08:00
if hooks != nil && hooks . AfterStart != nil {
if err := hooks . AfterStart ( appInstall , t ); err != nil {
return err
}
}
2024-07-29 18:09:57 +08:00
updateToolApp ( appInstall )
return nil
}
2024-08-02 16:00:15 +08:00
handleAppStatus := func ( t * task . Task ) {
2025-03-04 15:18:04 +08:00
appInstall . Status = constant . StatusUpErr
2024-07-29 18:09:57 +08:00
appInstall . Message = installTask . Task . ErrorMsg
2026-11-13 18:20:13 +08:00
_ = appInstallRepo . UpdateStatus ( appInstall , false )
2024-07-29 18:09:57 +08:00
}
2026-11-01 23:36:15 +08:00
installTimeout := time . Hour
if app . Key == "ollama" || app . Key == "vllm" {
installTimeout = 0
}
installTask . AddSubTaskWithOps ( task . GetTaskName ( appInstall . Name , task . TaskInstall , task . TaskScopeApp ), installApp , handleAppStatus , 0 , installTimeout )
2024-07-29 18:09:57 +08:00
2024-07-23 14:48:37 +08:00
go func () {
2026-11-25 22:25:08 +08:00
if hooks != nil && hooks . AfterFinish != nil {
defer hooks . AfterFinish ()
}
2024-07-29 18:09:57 +08:00
if taskErr := installTask . Execute (); taskErr != nil {
2025-03-04 15:18:04 +08:00
appInstall . Status = constant . StatusInstallErr
2024-07-29 18:09:57 +08:00
appInstall . Message = taskErr . Error ()
2025-01-09 17:02:22 +08:00
if strings . Contains ( taskErr . Error (), "Timeout" ) && strings . Contains ( taskErr . Error (), "Pulling" ) {
appInstall . Message = buserr . New ( "PullImageTimeout" ). Error () + appInstall . Message
}
2026-11-13 18:20:13 +08:00
_ = appInstallRepo . UpdateStatus ( appInstall , false )
2024-07-23 14:48:37 +08:00
}
}()
2024-07-29 18:09:57 +08:00
2024-07-23 14:48:37 +08:00
return
}
2024-08-23 17:22:05 +08:00
func ( a AppService ) SyncAppListFromLocal ( TaskID string ) {
2024-07-23 14:48:37 +08:00
var (
err error
dirEntries [] os . DirEntry
localApps [] model . App
)
2024-08-23 17:22:05 +08:00
syncTask , err := task . NewTaskWithOps ( i18n . GetMsgByKey ( "LocalApp" ), task . TaskSync , task . TaskScopeAppStore , TaskID , 0 )
2024-07-23 14:48:37 +08:00
if err != nil {
2024-08-23 17:22:05 +08:00
global . LOG . Errorf ( "Create sync task failed %v" , err )
2024-07-23 14:48:37 +08:00
return
}
2024-08-23 17:22:05 +08:00
syncTask . AddSubTask ( task . GetTaskName ( i18n . GetMsgByKey ( "LocalApp" ), task . TaskSync , task . TaskScopeAppStore ), func ( t * task . Task ) ( err error ) {
fileOp := files . NewFileOp ()
2025-01-24 11:00:07 +08:00
localAppDir := global . Dir . LocalAppResourceDir
2024-08-23 17:22:05 +08:00
if ! fileOp . Stat ( localAppDir ) {
return nil
2024-07-23 14:48:37 +08:00
}
2024-08-23 17:22:05 +08:00
dirEntries , err = os . ReadDir ( localAppDir )
if err != nil {
return
2024-07-23 14:48:37 +08:00
}
2024-08-23 17:22:05 +08:00
for _ , dirEntry := range dirEntries {
if dirEntry . IsDir () {
appDir := filepath . Join ( localAppDir , dirEntry . Name ())
appDirEntries , err := os . ReadDir ( appDir )
if err != nil {
t . Log ( i18n . GetWithNameAndErr ( "ErrAppDirNull" , dirEntry . Name (), err ))
2024-07-23 14:48:37 +08:00
continue
}
2024-08-23 17:22:05 +08:00
app , err := handleLocalApp ( appDir )
if err != nil {
t . Log ( i18n . GetWithNameAndErr ( "LocalAppErr" , dirEntry . Name (), err ))
continue
}
var appDetails [] model . AppDetail
for _ , appDirEntry := range appDirEntries {
if appDirEntry . IsDir () {
appDetail := model . AppDetail {
Version : appDirEntry . Name (),
Status : constant . AppNormal ,
}
versionDir := filepath . Join ( appDir , appDirEntry . Name ())
if err = handleLocalAppDetail ( versionDir , & appDetail ); err != nil {
t . Log ( i18n . GetMsgWithMap ( "LocalAppVersionErr" , map [ string ] interface {}{ "name" : app . Name , "version" : appDetail . Version , "err" : err . Error ()}))
continue
}
appDetails = append ( appDetails , appDetail )
}
}
if len ( appDetails ) > 0 {
app . Details = appDetails
localApps = append ( localApps , * app )
2024-07-23 14:48:37 +08:00
} else {
2024-08-23 17:22:05 +08:00
t . Log ( i18n . GetWithName ( "LocalAppVersionNull" , app . Name ))
2024-07-23 14:48:37 +08:00
}
}
}
2024-08-23 17:22:05 +08:00
var (
newApps [] model . App
deleteApps [] model . App
updateApps [] model . App
oldAppIds [] uint
deleteAppIds [] uint
deleteAppDetails [] model . AppDetail
newAppDetails [] model . AppDetail
updateDetails [] model . AppDetail
appTags [] * model . AppTag
)
oldApps , _ := appRepo . GetBy ( appRepo . WithResource ( constant . AppResourceLocal ))
apps := make ( map [ string ] model . App , len ( oldApps ))
for _ , old := range oldApps {
old . Status = constant . AppTakeDown
apps [ old . Key ] = old
}
for _ , app := range localApps {
if oldApp , ok := apps [ app . Key ]; ok {
app . ID = oldApp . ID
appDetails := make ( map [ string ] model . AppDetail , len ( oldApp . Details ))
for _ , old := range oldApp . Details {
old . Status = constant . AppTakeDown
appDetails [ old . Version ] = old
}
for i , newDetail := range app . Details {
version := newDetail . Version
newDetail . Status = constant . AppNormal
newDetail . AppId = app . ID
oldDetail , exist := appDetails [ version ]
if exist {
newDetail . ID = oldDetail . ID
delete ( appDetails , version )
}
app . Details [ i ] = newDetail
}
for _ , v := range appDetails {
app . Details = append ( app . Details , v )
}
}
app . TagsKey = append ( app . TagsKey , constant . AppResourceLocal )
apps [ app . Key ] = app
}
for _ , app := range apps {
if app . ID == 0 {
newApps = append ( newApps , app )
} else {
oldAppIds = append ( oldAppIds , app . ID )
if app . Status == constant . AppTakeDown {
2025-03-12 17:44:03 +08:00
installs , _ := appInstallRepo . ListBy ( context . Background (), appInstallRepo . WithAppId ( app . ID ))
2024-08-23 17:22:05 +08:00
if len ( installs ) > 0 {
updateApps = append ( updateApps , app )
continue
}
deleteAppIds = append ( deleteAppIds , app . ID )
deleteApps = append ( deleteApps , app )
deleteAppDetails = append ( deleteAppDetails , app . Details ... )
} else {
updateApps = append ( updateApps , app )
}
}
}
tags , _ := tagRepo . All ()
tagMap := make ( map [ string ] uint , len ( tags ))
for _ , tag := range tags {
tagMap [ tag . Key ] = tag . ID
}
tx , ctx := getTxAndContext ()
defer tx . Rollback ()
if len ( newApps ) > 0 {
if err = appRepo . BatchCreate ( ctx , newApps ); err != nil {
return
2024-07-23 14:48:37 +08:00
}
}
2024-08-23 17:22:05 +08:00
for _ , update := range updateApps {
if err = appRepo . Save ( ctx , & update ); err != nil {
return
}
2024-07-23 14:48:37 +08:00
}
2024-08-23 17:22:05 +08:00
if len ( deleteApps ) > 0 {
if err = appRepo . BatchDelete ( ctx , deleteApps ); err != nil {
return
}
if err = appDetailRepo . DeleteByAppIds ( ctx , deleteAppIds ); err != nil {
return
}
2024-07-23 14:48:37 +08:00
}
if err = appTagRepo . DeleteByAppIds ( ctx , oldAppIds ); err != nil {
return
}
2024-08-23 17:22:05 +08:00
for _ , newApp := range newApps {
if newApp . ID > 0 {
for _ , detail := range newApp . Details {
detail . AppId = newApp . ID
newAppDetails = append ( newAppDetails , detail )
}
}
2024-07-23 14:48:37 +08:00
}
2024-08-23 17:22:05 +08:00
for _ , update := range updateApps {
for _ , detail := range update . Details {
if detail . ID == 0 {
detail . AppId = update . ID
newAppDetails = append ( newAppDetails , detail )
} else {
if detail . Status == constant . AppNormal {
updateDetails = append ( updateDetails , detail )
} else {
deleteAppDetails = append ( deleteAppDetails , detail )
}
}
}
}
allApps := append ( newApps , updateApps ... )
for _ , app := range allApps {
for _ , t := range app . TagsKey {
tagId , ok := tagMap [ t ]
if ok {
appTags = append ( appTags , & model . AppTag {
AppId : app . ID ,
TagId : tagId ,
})
}
}
}
if len ( newAppDetails ) > 0 {
if err = appDetailRepo . BatchCreate ( ctx , newAppDetails ); err != nil {
return
}
}
for _ , updateAppDetail := range updateDetails {
if err = appDetailRepo . Update ( ctx , updateAppDetail ); err != nil {
return
}
}
if len ( deleteAppDetails ) > 0 {
if err = appDetailRepo . BatchDelete ( ctx , deleteAppDetails ); err != nil {
return
}
}
if len ( oldAppIds ) > 0 {
if err = appTagRepo . DeleteByAppIds ( ctx , oldAppIds ); err != nil {
return
}
}
if len ( appTags ) > 0 {
if err = appTagRepo . BatchCreate ( ctx , appTags ); err != nil {
return
}
}
tx . Commit ()
global . LOG . Infof ( "Synchronization of local applications completed" )
return nil
}, nil )
go func () {
_ = syncTask . Execute ()
}()
2024-07-23 14:48:37 +08:00
}
func ( a AppService ) GetAppUpdate () ( * response . AppUpdateRes , error ) {
res := & response . AppUpdateRes {
CanUpdate : false ,
}
2026-01-09 15:17:52 +08:00
mysql , _ := appRepo . GetFirst ( appRepo . WithKey ( "mysql" ))
if ! mysql . BatchInstallSupport {
res . CanUpdate = true
return res , nil
}
2024-07-23 14:48:37 +08:00
2026-02-25 10:56:06 +08:00
versionUrl := fmt . Sprintf ( "%s/%s/1panel.json.version.txt" , global . AppRepoURL (), global . CONF . Base . Mode )
2025-01-22 14:28:22 +08:00
_ , versionRes , err := req_helper . HandleRequest ( versionUrl , http . MethodGet , constant . TimeOut20s )
2024-07-23 14:48:37 +08:00
if err != nil {
return nil , err
}
lastModifiedStr := string ( versionRes )
lastModified , err := strconv . Atoi ( lastModifiedStr )
if err != nil {
return nil , err
}
setting , err := NewISettingService (). GetSettingInfo ()
if err != nil {
return nil , err
}
2025-03-04 15:18:04 +08:00
if setting . AppStoreSyncStatus == constant . StatusSyncing {
2024-07-23 14:48:37 +08:00
res . IsSyncing = true
return res , nil
}
appStoreLastModified , _ := strconv . Atoi ( setting . AppStoreLastModified )
res . AppStoreLastModified = appStoreLastModified
if setting . AppStoreLastModified == "" || lastModified != appStoreLastModified {
res . CanUpdate = true
return res , err
}
2026-10-30 18:14:41 +08:00
list , err := readAppList ()
if err != nil || list . LastModified != lastModified {
list , err = getAppList ()
2024-07-23 14:48:37 +08:00
}
if err != nil {
return res , err
}
if list . Extra . Version != "" && setting . SystemVersion != list . Extra . Version && ! common . CompareVersion ( setting . SystemVersion , list . Extra . Version ) {
2024-12-23 22:05:44 +08:00
global . LOG . Errorf ( "The current version %s is too low to synchronize with the App Store. The minimum required version is %s" , setting . SystemVersion , list . Extra . Version )
2024-07-23 14:48:37 +08:00
return nil , buserr . New ( "ErrVersionTooLow" )
}
res . AppList = list
return res , nil
}
func getAppFromRepo ( downloadPath string ) error {
downloadUrl := downloadPath
global . LOG . Infof ( "[AppStore] download file from %s" , downloadUrl )
fileOp := files . NewFileOp ()
2025-01-24 11:00:07 +08:00
packagePath := filepath . Join ( global . Dir . ResourceDir , filepath . Base ( downloadUrl ))
2025-04-30 18:41:52 +08:00
if err := files . DownloadFileWithProxy ( downloadUrl , packagePath ); err != nil {
2024-07-23 14:48:37 +08:00
return err
}
2025-04-30 18:41:52 +08:00
2026-04-28 15:31:40 +08:00
if err := fileOp . Decompress ( context . Background (), packagePath , global . Dir . ResourceDir , files . SdkZip , "" ); err != nil {
2024-07-23 14:48:37 +08:00
return err
}
defer func () {
_ = fileOp . DeleteFile ( packagePath )
}()
return nil
}
func getAppList () ( * dto . AppList , error ) {
2026-02-25 10:56:06 +08:00
if err := getAppFromRepo ( fmt . Sprintf ( "%s/%s/1panel.json.zip" , global . AppRepoURL (), global . CONF . Base . Mode )); err != nil {
2024-07-23 14:48:37 +08:00
return nil , err
}
2026-10-30 18:14:41 +08:00
return readAppList ()
}
func readAppList () ( * dto . AppList , error ) {
2025-01-24 11:00:07 +08:00
listFile := filepath . Join ( global . Dir . ResourceDir , "1panel.json" )
2026-10-30 18:14:41 +08:00
data , err := os . ReadFile ( listFile )
2024-07-23 14:48:37 +08:00
if err != nil {
return nil , err
}
2026-10-30 18:14:41 +08:00
list := & dto . AppList {}
if err = json . Unmarshal ( data , list ); err != nil {
2024-07-23 14:48:37 +08:00
return nil , err
}
2025-11-27 13:47:03 +08:00
2024-07-23 14:48:37 +08:00
return list , nil
}
2025-01-03 16:54:09 +08:00
func deleteCustomApp () {
2026-02-05 13:44:59 +08:00
installs , err := appInstallRepo . ListBy ( context . Background ())
if err != nil {
global . LOG . Errorf ( "[AppStore] deleteCustomApp: failed to list installs, skipping: %v" , err )
return
}
2025-01-03 16:54:09 +08:00
var appIDS [] uint
for _ , install := range installs {
appIDS = append ( appIDS , install . AppId )
}
var ops [] repo . DBOption
if len ( appIDS ) > 0 {
ops = append ( ops , repo . WithByIDNotIn ( appIDS ))
}
2026-02-05 13:44:59 +08:00
apps , err := appRepo . GetBy ( ops ... )
if err != nil {
global . LOG . Errorf ( "[AppStore] deleteCustomApp: failed to get apps, skipping: %v" , err )
return
}
2025-01-03 16:54:09 +08:00
var deleteIDS [] uint
for _ , app := range apps {
if app . Resource == constant . AppResourceCustom {
deleteIDS = append ( deleteIDS , app . ID )
}
}
2026-02-05 13:44:59 +08:00
if len ( deleteIDS ) == 0 {
return
}
if err = appRepo . DeleteByIDs ( context . Background (), deleteIDS ); err != nil {
global . LOG . Errorf ( "[AppStore] deleteCustomApp: failed to delete apps: %v" , err )
}
if err = appDetailRepo . DeleteByAppIds ( context . Background (), deleteIDS ); err != nil {
global . LOG . Errorf ( "[AppStore] deleteCustomApp: failed to delete app details: %v" , err )
}
2025-01-03 16:54:09 +08:00
}
2024-08-22 18:48:55 +08:00
func ( a AppService ) SyncAppListFromRemote ( taskID string ) ( err error ) {
2026-04-23 17:00:50 +08:00
if xpack . MultiNodeProvider . IsUseCustomApp () {
2024-12-30 21:04:11 +08:00
return nil
}
2026-02-05 13:44:59 +08:00
appStoreSyncMu . Lock ()
global . LOG . Info ( "[AppStore] sync app from remote task create start" )
if appStoreSyncing {
appStoreSyncMu . Unlock ()
global . LOG . Info ( "[AppStore] sync already in progress, skipping" )
return nil
}
appStoreSyncing = true
appStoreSyncMu . Unlock ()
2024-08-22 18:48:55 +08:00
syncTask , err := task . NewTaskWithOps ( i18n . GetMsgByKey ( "App" ), task . TaskSync , task . TaskScopeAppStore , taskID , 0 )
2024-07-23 14:48:37 +08:00
if err != nil {
2026-02-05 13:44:59 +08:00
appStoreSyncMu . Lock ()
appStoreSyncing = false
appStoreSyncMu . Unlock ()
2024-07-23 14:48:37 +08:00
return err
}
2026-01-27 14:02:38 +08:00
var sharedCtx * appSyncContext
syncTask . AddSubTask ( task . GetTaskName ( i18n . GetMsgByKey ( "App" ), task . TaskSync , task . TaskScopeAppStore ), a . createSyncAppStoreTask ( & sharedCtx ), nil )
syncTask . AddSubTask ( i18n . GetMsgByKey ( "SyncAppDetail" ), a . createSyncAppStoreMetaTask ( & sharedCtx ), nil )
2024-08-22 18:48:55 +08:00
2024-08-26 13:40:27 +08:00
go func () {
2026-02-05 13:44:59 +08:00
defer func () {
if r := recover (); r != nil {
global . LOG . Errorf ( "[AppStore] sync goroutine recovered from panic: %v" , r )
if updateErr := NewISettingService (). Update ( "AppStoreSyncStatus" , constant . StatusError ); updateErr != nil {
global . LOG . Warnf ( "[AppStore] failed to update sync status after panic: %v" , updateErr )
}
}
appStoreSyncMu . Lock ()
appStoreSyncing = false
appStoreSyncMu . Unlock ()
}()
2025-12-03 10:08:20 +08:00
if err := syncTask . Execute (); err != nil {
2026-02-05 13:44:59 +08:00
if updateErr := NewISettingService (). Update ( "AppStoreLastModified" , "0" ); updateErr != nil {
global . LOG . Warnf ( "[AppStore] failed to reset last modified: %v" , updateErr )
}
if updateErr := NewISettingService (). Update ( "AppStoreSyncStatus" , constant . StatusError ); updateErr != nil {
global . LOG . Warnf ( "[AppStore] failed to update sync status to error: %v" , updateErr )
}
2026-01-27 14:02:38 +08:00
return
2024-08-26 13:40:27 +08:00
}
}()
2026-02-05 13:44:59 +08:00
global . LOG . Info ( "[AppStore] sync app from remote task create ok" )
2024-08-26 13:40:27 +08:00
return nil
2024-07-23 14:48:37 +08:00
}
2025-11-20 18:57:49 +08:00
2026-01-27 14:02:38 +08:00
func ( a AppService ) GetAppIcon ( key string ) ([] byte , string , string , error ) {
2026-01-09 16:41:52 +08:00
app , err := appRepo . GetFirst ( appRepo . WithKey ( key ))
2025-11-20 18:57:49 +08:00
if err != nil {
2026-01-27 14:02:38 +08:00
return nil , "" , "" , err
2025-11-20 18:57:49 +08:00
}
2026-01-27 14:02:38 +08:00
2026-10-30 18:14:41 +08:00
if appicon . IsRemoteURL ( app . Icon ) {
return nil , app . Icon , "" , nil
}
2026-01-27 14:02:38 +08:00
if appicon . IsIconFile ( app . Icon ) {
fileName , etag := appicon . ParseIconField ( app . Icon )
iconBytes , err := appicon . ReadIconFile ( fileName )
if err != nil {
global . LOG . Warnf ( "[AppIcon] read icon file failed key=%s, file=%s, err=%v" , key , fileName , err )
return nil , "" , "" , nil
}
return iconBytes , fileName , etag , nil
}
2025-11-20 18:57:49 +08:00
iconBytes , err := base64 . StdEncoding . DecodeString ( app . Icon )
if err != nil {
2026-01-27 14:02:38 +08:00
global . LOG . Warnf ( "[AppIcon] decode base64 icon failed key=%s, err=%v" , key , err )
return nil , "" , "" , nil
2025-11-20 18:57:49 +08:00
}
2026-01-27 14:02:38 +08:00
return iconBytes , "" , "" , nil
2025-11-20 18:57:49 +08:00
}