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

Method Create

agent/app/service/file_share.go:130–213  ·  view source on GitHub ↗
(req request.FileShareCreate)

Source from the content-addressed store, hash-verified

128}
129
130func (s *FileShareService) Create(req request.FileShareCreate) (*response.FileShareInfo, error) {
131 path := strings.TrimSpace(req.Path)
132 if path == "" || strings.Contains(path, "..") {
133 return nil, buserr.New("ErrFileSharePath")
134 }
135 if files.ShouldDenySensitiveFileRead(path) {
136 return nil, buserr.New("ErrSensitiveFileRead")
137 }
138 info, err := os.Stat(path)
139 if err != nil || info.IsDir() {
140 return nil, buserr.New("ErrFileSharePath")
141 }
142
143 item, err := fileShareRepo.GetFirst(fileShareRepo.WithByPath(path))
144 if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
145 return nil, err
146 }
147
148 isNew := errors.Is(err, gorm.ErrRecordNotFound)
149 if isNew {
150 code, err := s.generateUniqueCode()
151 if err != nil {
152 return nil, err
153 }
154 item = model.FileShare{
155 Path: path,
156 Token: code,
157 FileName: filepath.Base(path),
158 }
159 } else if !fileShareCodeRegexp.MatchString(item.Token) {
160 code, err := s.generateUniqueCode()
161 if err != nil {
162 return nil, err
163 }
164 item.Token = code
165 }
166
167 item.FileName = filepath.Base(path)
168 item.MaxDownloads = 0
169 item.DownloadCount = 0
170 item.ExpiresUnix = 0
171 if req.ExpireMinutes > 0 {
172 item.ExpiresUnix = time.Now().Add(time.Duration(req.ExpireMinutes) * time.Minute).Unix()
173 }
174
175 if req.Password != nil {
176 pw := strings.TrimSpace(*req.Password)
177 if pw == "" {
178 item.PasswordEnc = ""
179 item.PasswordSalt = ""
180 item.PasswordHash = ""
181 } else {
182 pwLen := utf8.RuneCountInString(pw)
183 if pwLen < 4 || pwLen > 256 {
184 return nil, buserr.New("ErrFileSharePasswordPolicy")
185 }
186 enc, err := encrypt.StringEncrypt(pw)
187 if err != nil {

Callers

nothing calls this directly

Calls 12

generateUniqueCodeMethod · 0.95
randomSaltFunction · 0.85
hashPasswordFunction · 0.85
shareModelToInfoFunction · 0.85
fillSharePasswordFunction · 0.85
IsDirMethod · 0.80
IsMethod · 0.80
GetFirstMethod · 0.65
WithByPathMethod · 0.65
CreateMethod · 0.65
SaveMethod · 0.65
StatMethod · 0.45

Tested by

no test coverage detected