ZYLUploadFile.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // ZYLUploadFile.m
  3. // NetworkLayerBuild
  4. //
  5. // Created by 翟玉磊 on 2017/8/31.
  6. // Copyright © 2017年 翟玉磊. All rights reserved.
  7. //
  8. #import "ZYLUploadFile.h"
  9. #import "ZYLAPPEncryption.h"
  10. @interface ZYLUploadFile ()
  11. @property (nonatomic, strong) NSData *fileData;
  12. @property (nonatomic, copy) NSString *fileName;
  13. @property (nonatomic, copy) NSString *fileType;
  14. @property (nonatomic, copy) NSString *uploadKey;
  15. @property (nonatomic, copy) NSString *md5String;
  16. @end
  17. @implementation ZYLUploadFile
  18. - (instancetype)initWithFileData:(NSData *)data fileName:(NSString *)name fileType:(ZYLUploadFileType)type uploadKey:(NSString *)key {
  19. if (self = [super init]) {
  20. self.fileData = data;
  21. self.uploadKey = key;
  22. switch (type) {
  23. case ZYLUploadFileTypePng:
  24. self.fileType = @"image/png";
  25. self.fileName = [name stringByAppendingString:@".png"];
  26. break;
  27. case ZYLUploadFileTypeJpg:
  28. self.fileType = @"image/jpeg";
  29. self.fileName = [name stringByAppendingString:@".jpeg"];
  30. break;
  31. case ZYLUploadFileTypeMp3:
  32. self.fileType = @"audio/mp3";
  33. self.fileName = [name stringByAppendingString:@".mp3"];
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. return self;
  40. }
  41. + (instancetype)pngImageWithFileData:(NSData *)data imageName:(NSString *)name {
  42. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypePng uploadKey:nil];
  43. }
  44. + (instancetype)jpgImageWithFileData:(NSData *)data imageName:(NSString *)name {
  45. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeJpg uploadKey:nil];
  46. }
  47. + (instancetype)mp3AudioWithFileData:(NSData *)data audioName:(NSString *)name {
  48. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeMp3 uploadKey:nil];
  49. }
  50. + (instancetype)pngImageWithFileData:(NSData *)data imageName:(NSString *)name uploadKey:(NSString *)key {
  51. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypePng uploadKey:key];
  52. }
  53. + (instancetype)jpgImageWithFileData:(NSData *)data imageName:(NSString *)name uploadKey:(NSString *)key {
  54. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeJpg uploadKey:key];
  55. }
  56. + (instancetype)mp3AudioWithFileData:(NSData *)data audioName:(NSString *)name uploadKey:(NSString *)key {
  57. return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeMp3 uploadKey:key];
  58. }
  59. #pragma mark - Getter
  60. - (NSString *)uploadKey {
  61. //如果没有设置则使用data的MD5值做key
  62. if (!_uploadKey) {
  63. _uploadKey = self.md5String;
  64. }
  65. return _uploadKey;
  66. }
  67. - (NSString *)md5String {
  68. if (!_md5String) {
  69. _md5String = self.fileData.length > 0 ? [ZYLAPPEncryption md5WithData:self.fileData] : @"";
  70. }
  71. return _md5String;
  72. }
  73. //描述实例
  74. - (NSString *)description {
  75. return [NSString stringWithFormat:@"\nfileName: %@\nfileType: %@\nuploadKey: %@\nfileLength: %ld",self.fileName,self.fileType,self.uploadKey,self.fileData.length];
  76. }
  77. @end