CWFileStreamSeparation.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // CWFileStreamSeparation.h
  3. // uploadFileDemo
  4. //
  5. // Created by hyjet on 2018/3/9.
  6. // Copyright © 2018年 uploadFileDemo. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. typedef enum : NSUInteger {
  10. CWUploadStatusWaiting = 0,//任务队列等待
  11. CWUploadStatusUpdownloading,//上传中
  12. CWUploadStatusPaused,//暂停
  13. CWUploadStatusFinished,//上传成功
  14. CWUploadStatusFailed //上传失败
  15. } CWUploadStatus;//任务状态
  16. #define CWStreamFragmentMaxSize 1024 * 1024 // 500KB
  17. @class CWStreamFragment;
  18. /**
  19. * 文件流操作类
  20. */
  21. @interface CWFileStreamSeparation : NSObject<NSCoding>
  22. @property (nonatomic, copy) NSString *fileName;//包括文件后缀名的文件名
  23. @property (nonatomic, strong) NSData *imageData; //文件预览图
  24. @property (nonatomic, assign) NSInteger timeStamp; //时间戳
  25. @property (nonatomic, assign) NSUInteger fileSize;//文件大小
  26. @property (nonatomic, copy) NSString *filePath;;//文件所在的文件目录
  27. @property (nonatomic, copy) NSString *uploadLocation;;//上传到服务器的文件目录
  28. @property (nonatomic, assign)CWUploadStatus fileStatus;//文件状态
  29. @property (nonatomic, copy)NSString *md5String;//文件md5编码名称
  30. @property (nonatomic, strong) NSArray<CWStreamFragment*> *streamFragments;//文件分片数组
  31. @property (nonatomic, copy) NSString *bizId;
  32. @property (nonatomic,assign)double progressRate;//上传进度
  33. @property (nonatomic,assign)NSInteger uploadDateSize;//已上传文件大小
  34. @property (nonatomic,assign)NSInteger select; // 是否被选中
  35. /** description */
  36. @property (readonly, copy) NSString *description;
  37. //若为读取文件数据,打开一个已存在的文件。
  38. //若为写入文件数据,如果文件不存在,会创建的新的空文件。(创建FileStreamer对象就可以直接使用fragments(分片数组)属性)
  39. - (instancetype)initFileOperationAtPath:(NSString*)path forReadOperation:(BOOL)isReadOperation;
  40. - (instancetype)initFileOperationAtPath:(NSString*)path forReadOperation:(BOOL)isReadOperation uploadLocation:(NSString *)uploadLocation imageData:(NSData *)imageData;
  41. //获取当前偏移量
  42. - (NSUInteger)offsetInFile;
  43. //设置偏移量, 仅对读取设置
  44. - (void)seekToFileOffset:(NSUInteger)offset;
  45. //将偏移量定位到文件的末尾
  46. - (NSUInteger)seekToEndOfFile;
  47. //关闭文件
  48. - (void)closeFile;
  49. #pragma mark - 读操作
  50. //通过分片信息读取对应的片数据
  51. - (NSData*)readDateOfFragment:(CWStreamFragment*)fragment;
  52. //从当前文件偏移量开始
  53. - (NSData*)readDataOfLength:(NSUInteger)bytes;
  54. //从当前文件偏移量开始
  55. - (NSData*)readDataToEndOfFile;
  56. #pragma mark - 写操作
  57. //写入文件数据
  58. - (void)writeData:(NSData *)data;
  59. +(NSString*)fileKeyMD5WithPath:(NSString*)path;
  60. @end
  61. //上传文件片
  62. @interface CWStreamFragment : NSObject<NSCoding>
  63. @property (nonatomic,copy)NSString *fragmentId; //片的唯一标识
  64. @property (nonatomic,assign)NSUInteger fragmentSize; //片的大小
  65. @property (nonatomic,assign)NSUInteger fragementOffset;//片的偏移量
  66. @property (nonatomic,assign)BOOL fragmentStatus; //上传状态 YES上传成功
  67. @end