AFDownloadRequestOperation.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // AFDownloadRequestOperation.h
  2. //
  3. // Copyright (c) 2012 Peter Steinberger (http://petersteinberger.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "AFHTTPRequestOperation.h"
  23. #define kAFNetworkingIncompleteDownloadFolderName @"Incomplete"
  24. /**
  25. `AFDownloadRequestOperation` is a subclass of `AFHTTPRequestOperation` for streamed file downloading. Supports Content-Range. (http://tools.ietf.org/html/rfc2616#section-14.16)
  26. */
  27. @interface AFDownloadRequestOperation : AFHTTPRequestOperation
  28. /**
  29. A String value that defines the target path or directory.
  30. We try to be clever here and understand both a directory or a filename.
  31. The target directory should already be create, or the download fill fail.
  32. If the target is a directory, we use the last part of the URL as a default file name.
  33. targetPath is the responseObject if operation succeeds
  34. */
  35. @property (strong) NSString *targetPath;
  36. /**
  37. A Boolean value that indicates if we should allow a downloaded file to overwrite
  38. a previously downloaded file of the same name. Default is `NO`.
  39. */
  40. @property (assign) BOOL shouldOverwrite;
  41. /**
  42. A Boolean value that indicates if we should try to resume the download. Defaults is `YES`.
  43. Can only be set while creating the request.
  44. */
  45. @property (assign, readonly) BOOL shouldResume;
  46. /**
  47. Deletes the temporary file if operations is cancelled. Defaults to `NO`.
  48. */
  49. @property (assign, getter=isDeletingTempFileOnCancel) BOOL deleteTempFileOnCancel;
  50. /**
  51. Expected total length. This is different than expectedContentLength if the file is resumed.
  52. Note: this can also be zero if the file size is not sent (*)
  53. */
  54. @property (assign, readonly) long long totalContentLength;
  55. /**
  56. Indicator for the file offset on partial downloads. This is greater than zero if the file download is resumed.
  57. */
  58. @property (assign, readonly) long long offsetContentLength;
  59. /**
  60. The callback dispatch queue on progressive download. If `NULL` (default), the main queue is used.
  61. */
  62. @property (nonatomic, assign) dispatch_queue_t progressiveDownloadCallbackQueue;
  63. ///----------------------------------
  64. /// @name Creating Request Operations
  65. ///----------------------------------
  66. /**
  67. Creates and returns an `AFDownloadRequestOperation`
  68. @param urlRequest The request object to be loaded asynchronously during execution of the operation
  69. @param targetPath The target path (with or without file name)
  70. @param shouldResume If YES, tries to resume a partial download if found.
  71. @return A new download request operation
  72. */
  73. - (id)initWithRequest:(NSURLRequest *)urlRequest targetPath:(NSString *)targetPath shouldResume:(BOOL)shouldResume;
  74. /**
  75. Deletes the temporary file.
  76. Returns `NO` if an error happened, `YES` if the file is removed or did not exist in the first place.
  77. */
  78. - (BOOL)deleteTempFileWithError:(NSError **)error;
  79. /**
  80. Returns the path used for the temporary file. Returns `nil` if the targetPath has not been set.
  81. */
  82. - (NSString *)tempPath;
  83. /**
  84. Sets a callback to be called when an undetermined number of bytes have been downloaded from the server. This is a variant of setDownloadProgressBlock that adds support for progressive downloads and adds the
  85. @param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes five arguments: the number of bytes read since the last time the download progress block was called, the bytes expected to be read during the request, the bytes already read during this request, the total bytes read (including from previous partial downloads), and the total bytes expected to be read for the file. This block may be called multiple times.
  86. @see setDownloadProgressBlock
  87. */
  88. - (void)setProgressiveDownloadProgressBlock:(void (^)(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile))block;
  89. @end