123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- //
- // ZYLUploadFile.m
- // NetworkLayerBuild
- //
- // Created by 翟玉磊 on 2017/8/31.
- // Copyright © 2017年 翟玉磊. All rights reserved.
- //
- #import "ZYLUploadFile.h"
- #import "ZYLAPPEncryption.h"
- @interface ZYLUploadFile ()
- @property (nonatomic, strong) NSData *fileData;
- @property (nonatomic, copy) NSString *fileName;
- @property (nonatomic, copy) NSString *fileType;
- @property (nonatomic, copy) NSString *uploadKey;
- @property (nonatomic, copy) NSString *md5String;
- @end
- @implementation ZYLUploadFile
- - (instancetype)initWithFileData:(NSData *)data fileName:(NSString *)name fileType:(ZYLUploadFileType)type uploadKey:(NSString *)key {
- if (self = [super init]) {
- self.fileData = data;
- self.uploadKey = key;
-
- switch (type) {
- case ZYLUploadFileTypePng:
-
- self.fileType = @"image/png";
- self.fileName = [name stringByAppendingString:@".png"];
- break;
-
- case ZYLUploadFileTypeJpg:
-
- self.fileType = @"image/jpeg";
- self.fileName = [name stringByAppendingString:@".jpeg"];
- break;
-
- case ZYLUploadFileTypeMp3:
-
- self.fileType = @"audio/mp3";
- self.fileName = [name stringByAppendingString:@".mp3"];
- break;
-
- default:
- break;
- }
- }
- return self;
- }
- + (instancetype)pngImageWithFileData:(NSData *)data imageName:(NSString *)name {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypePng uploadKey:nil];
- }
- + (instancetype)jpgImageWithFileData:(NSData *)data imageName:(NSString *)name {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeJpg uploadKey:nil];
- }
- + (instancetype)mp3AudioWithFileData:(NSData *)data audioName:(NSString *)name {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeMp3 uploadKey:nil];
- }
- + (instancetype)pngImageWithFileData:(NSData *)data imageName:(NSString *)name uploadKey:(NSString *)key {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypePng uploadKey:key];
- }
- + (instancetype)jpgImageWithFileData:(NSData *)data imageName:(NSString *)name uploadKey:(NSString *)key {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeJpg uploadKey:key];
- }
- + (instancetype)mp3AudioWithFileData:(NSData *)data audioName:(NSString *)name uploadKey:(NSString *)key {
- return [[ZYLUploadFile alloc] initWithFileData:data fileName:name fileType:ZYLUploadFileTypeMp3 uploadKey:key];
- }
- #pragma mark - Getter
- - (NSString *)uploadKey {
-
- //如果没有设置则使用data的MD5值做key
- if (!_uploadKey) {
- _uploadKey = self.md5String;
- }
- return _uploadKey;
- }
- - (NSString *)md5String {
- if (!_md5String) {
- _md5String = self.fileData.length > 0 ? [ZYLAPPEncryption md5WithData:self.fileData] : @"";
- }
- return _md5String;
- }
- //描述实例
- - (NSString *)description {
- return [NSString stringWithFormat:@"\nfileName: %@\nfileType: %@\nuploadKey: %@\nfileLength: %ld",self.fileName,self.fileType,self.uploadKey,self.fileData.length];
- }
- @end
|