(req request.FileShareCreate)
| 128 | } |
| 129 | |
| 130 | func (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 { |
nothing calls this directly
no test coverage detected