put implements Put or PutStream
(ctx context.Context, in io.Reader, src fs.ObjectInfo, options []fs.OpenOption, put putFn)
| 495 | |
| 496 | // put implements Put or PutStream |
| 497 | func (f *Fs) put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options []fs.OpenOption, put putFn) (fs.Object, error) { |
| 498 | ci := fs.GetConfig(ctx) |
| 499 | |
| 500 | if f.opt.NoDataEncryption { |
| 501 | o, err := put(ctx, in, f.newObjectInfo(src, nonce{}), options...) |
| 502 | if err == nil && o != nil { |
| 503 | o = f.newObject(o) |
| 504 | } |
| 505 | return o, err |
| 506 | } |
| 507 | |
| 508 | // Encrypt the data into wrappedIn |
| 509 | wrappedIn, encrypter, err := f.cipher.encryptData(in) |
| 510 | if err != nil { |
| 511 | return nil, err |
| 512 | } |
| 513 | |
| 514 | // Find a hash the destination supports to compute a hash of |
| 515 | // the encrypted data |
| 516 | ht := f.Fs.Hashes().GetOne() |
| 517 | if ci.IgnoreChecksum { |
| 518 | ht = hash.None |
| 519 | } |
| 520 | var hasher *hash.MultiHasher |
| 521 | if ht != hash.None { |
| 522 | hasher, err = hash.NewMultiHasherTypes(hash.NewHashSet(ht)) |
| 523 | if err != nil { |
| 524 | return nil, err |
| 525 | } |
| 526 | // unwrap the accounting |
| 527 | var wrap accounting.WrapFn |
| 528 | wrappedIn, wrap = accounting.UnWrap(wrappedIn) |
| 529 | // add the hasher |
| 530 | wrappedIn = io.TeeReader(wrappedIn, hasher) |
| 531 | // wrap the accounting back on |
| 532 | wrappedIn = wrap(wrappedIn) |
| 533 | } |
| 534 | |
| 535 | // Transfer the data |
| 536 | o, err := put(ctx, wrappedIn, f.newObjectInfo(src, encrypter.nonce), options...) |
| 537 | if err != nil { |
| 538 | return nil, err |
| 539 | } |
| 540 | |
| 541 | // Check the hashes of the encrypted data if we were comparing them |
| 542 | if ht != hash.None && hasher != nil { |
| 543 | srcHash := hasher.Sums()[ht] |
| 544 | var dstHash string |
| 545 | dstHash, err = o.Hash(ctx, ht) |
| 546 | if err != nil { |
| 547 | return nil, fmt.Errorf("failed to read destination hash: %w", err) |
| 548 | } |
| 549 | if srcHash != "" && dstHash != "" { |
| 550 | if srcHash != dstHash { |
| 551 | // remove object |
| 552 | err = o.Remove(ctx) |
| 553 | if err != nil { |
| 554 | fs.Errorf(o, "Failed to remove corrupted object: %v", err) |
no test coverage detected