UploadFile uploads the provided File to the fileKey location.
(file *File, fileKey string)
| 216 | |
| 217 | // UploadFile uploads the provided File to the fileKey location. |
| 218 | func (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. |
| 260 | func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error { |