MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / NewFileInfo

Function NewFileInfo

agent/utils/files/fileinfo.go:141–205  ·  view source on GitHub ↗
(op FileOption)

Source from the content-addressed store, hash-verified

139}
140
141func NewFileInfo(op FileOption) (*FileInfo, error) {
142 var appFs = afero.NewOsFs()
143
144 info, err := appFs.Stat(op.Path)
145 if err != nil {
146 if os.IsNotExist(err) {
147 return nil, buserr.New("ErrLinkPathNotFound")
148 }
149 return nil, err
150 }
151
152 file := &FileInfo{
153 Fs: appFs,
154 Path: op.Path,
155 Name: info.Name(),
156 IsDir: info.IsDir(),
157 FileMode: info.Mode(),
158 ModTime: info.ModTime(),
159 Size: info.Size(),
160 IsSymlink: IsSymlink(info.Mode()),
161 Extension: filepath.Ext(info.Name()),
162 IsHidden: IsHidden(op.Path),
163 Mode: fmt.Sprintf("%04o", info.Mode().Perm()),
164 User: GetUsername(info.Sys().(*syscall.Stat_t).Uid),
165 Uid: strconv.FormatUint(uint64(info.Sys().(*syscall.Stat_t).Uid), 10),
166 Gid: strconv.FormatUint(uint64(info.Sys().(*syscall.Stat_t).Gid), 10),
167 Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid),
168 MimeType: GetMimeType(op.Path),
169 IsDetail: op.IsDetail,
170 }
171 favoriteRepo := repo.NewIFavoriteRepo()
172 favorite, _ := favoriteRepo.GetFirst(favoriteRepo.WithByPath(op.Path))
173 if favorite.ID > 0 {
174 file.FavoriteID = favorite.ID
175 }
176
177 if file.IsSymlink {
178 linkPath := GetSymlink(op.Path)
179 if !filepath.IsAbs(linkPath) {
180 dir := filepath.Dir(op.Path)
181 var err error
182 linkPath, err = filepath.Abs(filepath.Join(dir, linkPath))
183 if err != nil {
184 return nil, err
185 }
186 }
187 file.LinkPath = linkPath
188 targetInfo, err := appFs.Stat(linkPath)
189 if err != nil {
190 file.IsDir = false
191 file.Mode = "-"
192 file.User = "-"
193 file.Group = "-"
194 } else {
195 file.IsDir = targetInfo.IsDir()
196 }
197 file.Extension = filepath.Ext(file.LinkPath)
198 }

Callers

nothing calls this directly

Calls 15

GetFirstMethod · 0.95
WithByPathMethod · 0.95
IsSymlinkFunction · 0.85
IsHiddenFunction · 0.85
GetUsernameFunction · 0.85
GetGroupFunction · 0.85
GetMimeTypeFunction · 0.85
GetSymlinkFunction · 0.85
handleExpansionFunction · 0.85
IsDirMethod · 0.80
ModeMethod · 0.80
ModTimeMethod · 0.80

Tested by

no test coverage detected