MCDownloadReceipt.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MCDownloadReceipt.h
  3. // MCDownloadManager
  4. //
  5. // Created by M.C on 17/4/6. (QQ:714080794 Gmail:chaoma0609@gmail.com)
  6. // Copyright © 2017年 qikeyun. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. @class MCDownloadReceipt;
  28. /** The download state */
  29. typedef NS_ENUM(NSUInteger, MCDownloadState) {
  30. MCDownloadStateNone, /** default */
  31. MCDownloadStateWillResume, /** waiting */
  32. MCDownloadStateDownloading, /** downloading */
  33. MCDownloadStateSuspened, /** suspened */
  34. MCDownloadStateCompleted, /** download completed */
  35. MCDownloadStateFailed /** download failed */
  36. };
  37. /** The download prioritization */
  38. typedef NS_ENUM(NSInteger, MCDownloadPrioritization) {
  39. MCDownloadPrioritizationFIFO, /** first in first out */
  40. MCDownloadPrioritizationLIFO /** last in first out */
  41. };
  42. typedef void(^MCDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSInteger speed, NSURL * _Nullable targetURL);
  43. typedef void(^MCDownloaderCompletedBlock)(MCDownloadReceipt * _Nullable receipt, NSError * _Nullable error, BOOL finished);
  44. /**
  45. * The receipt of a downloader,we can get all the informations from the receipt.
  46. */
  47. @interface MCDownloadReceipt : NSObject
  48. /**
  49. * Download State
  50. */
  51. @property (nonatomic, assign, readonly) MCDownloadState state;
  52. /**
  53. The download source url
  54. */
  55. @property (nonatomic, copy, readonly, nonnull) NSString *url;
  56. /**
  57. The file path, you can use it to get the downloaded data.
  58. */
  59. @property (nonatomic, copy, readonly, nonnull) NSString *filePath;
  60. /**
  61. The url's pathExtension through the MD5 processing.
  62. */
  63. @property (nonatomic, copy, readonly, nullable) NSString *filename;
  64. /**
  65. The url's pathExtension without through the MD5 processing.
  66. */
  67. @property (nonatomic, copy, readonly, nullable) NSString *truename;
  68. /**
  69. The current download speed,
  70. */
  71. @property (nonatomic, copy, readonly, nullable) NSString *speed; // KB/s
  72. @property (assign, nonatomic, readonly) long long totalBytesWritten;
  73. @property (assign, nonatomic, readonly) long long totalBytesExpectedToWrite;
  74. /**
  75. The current download progress object
  76. */
  77. @property (nonatomic, strong, readonly, nullable) NSProgress *progress;
  78. @property (nonatomic, strong, readonly, nullable) NSError *error;
  79. /**
  80. The call back block. When setting this block,the progress block will be called during downloading,the complete block will be called after download finished.
  81. */
  82. @property (nonatomic,copy, nullable, readonly)MCDownloaderProgressBlock downloaderProgressBlock;
  83. @property (nonatomic,copy, nullable, readonly)MCDownloaderCompletedBlock downloaderCompletedBlock;
  84. #pragma mark - Private Methods
  85. ///=============================================================================
  86. /// Method is at the bottom of the private method, do not need to use
  87. ///=============================================================================
  88. /**
  89. The `MCDowmloadReceipt` method of initialization. Generally don't need to use this method.
  90. use `MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:url];` to get the `MCDowmloadReceipt`.
  91. */
  92. - (nonnull instancetype)initWithURLString:(nonnull NSString *)URLString
  93. downloadOperationCancelToken:(nullable id)downloadOperationCancelToken
  94. downloaderProgressBlock:(nullable MCDownloaderProgressBlock)downloaderProgressBlock
  95. downloaderCompletedBlock:(nullable MCDownloaderCompletedBlock)downloaderCompletedBlock;
  96. - (void)setTotalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite;
  97. - (void)setState:(MCDownloadState)state;
  98. - (void)setDownloadOperationCancelToken:(nullable id)downloadOperationCancelToken;
  99. - (void)setDownloaderProgressBlock:(nullable MCDownloaderProgressBlock)downloaderProgressBlock;
  100. - (void)setDownloaderCompletedBlock:(nullable MCDownloaderCompletedBlock)downloaderCompletedBlock;
  101. - (void)setSpeed:(NSString * _Nullable)speed;
  102. /**
  103. Auxiliary attributes and don't need to use
  104. */
  105. @property (nonatomic, assign) NSUInteger totalRead;
  106. @property (nonatomic, strong, nullable) NSDate *date;
  107. @property (nonatomic, strong, nullable) id downloadOperationCancelToken;
  108. @end