MCPcopy
hub / github.com/pocketbase/pocketbase / UploadFile

Method UploadFile

tools/filesystem/filesystem.go:218–257  ·  view source on GitHub ↗

UploadFile uploads the provided File to the fileKey location.

(file *File, fileKey string)

Source from the content-addressed store, hash-verified

216
217// UploadFile uploads the provided File to the fileKey location.
218func (s *System) UploadFile(file *File, fileKey string) error {
219 f, err := file.Reader.Open()
220 if err != nil {
221 return err
222 }
223 defer f.Close()
224
225 mt, err := mimetype.DetectReader(f)
226 if err != nil {
227 return err
228 }
229
230 // rewind
231 f.Seek(0, io.SeekStart)
232
233 originalName := file.OriginalName
234 if len(originalName) > 255 {
235 // keep only the first 255 chars as a very rudimentary measure
236 // to prevent the metadata to grow too big in size
237 originalName = originalName[:255]
238 }
239 opts := &blob.WriterOptions{
240 ContentType: mt.String(),
241 Metadata: map[string]string{
242 metadataOriginalName: originalName,
243 },
244 }
245
246 w, err := s.bucket.NewWriter(s.ctx, fileKey, opts)
247 if err != nil {
248 return err
249 }
250
251 if _, err := w.ReadFrom(f); err != nil {
252 w.Close()
253 return err
254 }
255
256 return w.Close()
257}
258
259// UploadMultipart uploads the provided multipart file to the fileKey location.
260func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error {

Callers 4

TestFileSystemUploadFileFunction · 0.80
CreateBackupMethod · 0.80
processFilesToUploadMethod · 0.80
backupUploadFunction · 0.80

Calls 7

ReadFromMethod · 0.95
CloseMethod · 0.95
SeekMethod · 0.80
NewWriterMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by 1

TestFileSystemUploadFileFunction · 0.64