Simplify Delivery.{Close,Abort}
Simon Ser
4 years ago
344 | 344 | |
345 | 345 | // Close closes the underlying file and moves it to new. |
346 | 346 | func (d *Delivery) Close() error { |
347 | tmppath := d.file.Name() | |
347 | 348 | err := d.file.Close() |
348 | 349 | if err != nil { |
349 | 350 | return err |
350 | 351 | } |
351 | err = os.Link(filepath.Join(string(d.d), "tmp", d.key), | |
352 | filepath.Join(string(d.d), "new", d.key)) | |
353 | if err != nil { | |
354 | return err | |
355 | } | |
356 | err = os.Remove(filepath.Join(string(d.d), "tmp", d.key)) | |
352 | err = os.Link(tmppath, filepath.Join(string(d.d), "new", d.key)) | |
353 | if err != nil { | |
354 | return err | |
355 | } | |
356 | err = os.Remove(tmppath) | |
357 | 357 | if err != nil { |
358 | 358 | return err |
359 | 359 | } |
362 | 362 | |
363 | 363 | // Abort closes the underlying file and removes it completely. |
364 | 364 | func (d *Delivery) Abort() error { |
365 | tmppath := d.file.Name() | |
365 | 366 | err := d.file.Close() |
366 | 367 | if err != nil { |
367 | 368 | return err |
368 | 369 | } |
369 | err = os.Remove(filepath.Join(string(d.d), "tmp", d.key)) | |
370 | err = os.Remove(tmppath) | |
370 | 371 | if err != nil { |
371 | 372 | return err |
372 | 373 | } |