summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/public/SDLFile.m
blob: 424e7c3b37f7814c2d7ea06a1daf2bc169881739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
//  SDLFile.m
//  SmartDeviceLink-iOS
//
//  Created by Joel Fischer on 10/14/15.
//  Copyright © 2015 smartdevicelink. All rights reserved.
//

#import "SDLFile.h"

#import "SDLFileManager.h"
#import "SDLFileType.h"


NS_ASSUME_NONNULL_BEGIN

@interface SDLFile ()

@property (copy, nonatomic, readwrite, nullable) NSURL *fileURL;
@property (copy, nonatomic, readwrite) NSData *data;

@property (strong, nonatomic, readwrite) SDLFileType fileType;
@property (assign, nonatomic, readwrite) BOOL persistent;
@property (copy, nonatomic, readwrite) NSString *name;

@property (nonatomic, readwrite) NSInputStream *inputStream;

@property (assign, nonatomic, readwrite) BOOL isStaticIcon;

@end


@implementation SDLFile

#pragma mark - Lifecycle

- (instancetype)initWithFileURL:(NSURL *)url name:(NSString *)name persistent:(BOOL)persistent {
    self = [super init];
    if (!self) {
        return nil;
    }

    BOOL exists = url.isFileURL && [url checkResourceIsReachableAndReturnError:nil];
    if (!exists) {
        return nil;
    }

    _fileURL = url;
    _data = [NSData data];
    _name = name;
    _persistent = persistent;
    _fileType = [self.class sdl_fileTypeFromFileExtension:url.pathExtension];
    _overwrite = NO;

    return self;
}

+ (instancetype)persistentFileAtFileURL:(NSURL *)url name:(NSString *)name {
    return [[self alloc] initWithFileURL:url name:name persistent:YES];
}

+ (instancetype)fileAtFileURL:(NSURL *)url name:(NSString *)name {
    return [[self alloc] initWithFileURL:url name:name persistent:NO];
}

- (instancetype)initWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension persistent:(BOOL)persistent {
    self = [super init];
    if (!self) {
        return nil;
    }
    if (data.length == 0) {
        return nil;
    }

    _fileURL = nil;
    _data = data;
    _name = name;
    _persistent = persistent;
    _fileType = [self.class sdl_fileTypeFromFileExtension:extension];
    _overwrite = NO;

    return self;
}

+ (instancetype)persistentFileWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension {
    return [[self alloc] initWithData:data name:name fileExtension:extension persistent:YES];
}

+ (instancetype)fileWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension {
    return [[self alloc] initWithData:data name:name fileExtension:extension persistent:NO];
}

#pragma mark - Getters

- (NSData *)data {
    if (_data.length == 0 && _fileURL != nil) {
        return [NSData dataWithContentsOfURL:_fileURL];
    }

    return _data;
}

/**
 Initalizes a socket from which to read data.

 @discussion A new `NSInputStream` is created when requested instead of returning the already open `NSInputStream`. This is done because once a opened, a stream *cannot* be closed and reopened. If the same file is accessed multiple times (i.e. a non-persistant file is uploaded before and after a disconnect from Core during the same session) the file cannot be accessed after the first access because the stream is closed once the upload is completed. Apple `NSInputStream` doc: https://developer.apple.com/documentation/foundation/nsstream/1411963-open

 @return A socket
 */
- (NSInputStream *)inputStream {
    if (_fileURL) {
        // Data in file
        _inputStream = [[NSInputStream alloc] initWithURL:_fileURL];
    } else if (_data.length != 0) {
        // Data in memory
        _inputStream = [[NSInputStream alloc] initWithData:_data];
    }
    return _inputStream;
}

/**
 Gets the size of the data. The data may be stored on disk or it may already be in the application's memory.

 @return The size of the data.
 */
- (unsigned long long)fileSize {
    if (_fileURL) {
        // Data in file
        NSString *path = [_fileURL path];
        return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
    } else if (_data) {
        // Data in memory
        return _data.length;
    }
    return 0;
}

#pragma mark - File Type

+ (SDLFileType)sdl_fileTypeFromFileExtension:(NSString *)fileExtension {
    if ([fileExtension caseInsensitiveCompare:@"bmp"] == NSOrderedSame) {
        return SDLFileTypeBMP;
    } else if (([fileExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame) ||
               ([fileExtension caseInsensitiveCompare:@"jpeg"] == NSOrderedSame)) {
        return SDLFileTypeJPEG;
    } else if ([fileExtension caseInsensitiveCompare:@"png"] == NSOrderedSame) {
        return SDLFileTypePNG;
    } else if ([fileExtension caseInsensitiveCompare:@"wav"] == NSOrderedSame) {
        return SDLFileTypeWAV;
    } else if ([fileExtension caseInsensitiveCompare:@"mp3"] == NSOrderedSame) {
        return SDLFileTypeMP3;
    } else if ([fileExtension caseInsensitiveCompare:@"aac"] == NSOrderedSame) {
        return SDLFileTypeAAC;
    } else if ([fileExtension caseInsensitiveCompare:@"json"] == NSOrderedSame) {
        return SDLFileTypeJSON;
    } else {
        return SDLFileTypeBinary;
    }
}

#pragma mark - NSCopying

- (id)copyWithZone:(nullable NSZone *)zone {
    SDLFile *fileCopy = [[self.class allocWithZone:zone] init];
    fileCopy.name = _name.copy;
    fileCopy.fileURL = _fileURL.copy;
    fileCopy.fileType = _fileType.copy;
    fileCopy.persistent = _persistent;
    fileCopy.isStaticIcon = _isStaticIcon;

    if (_data.length != 0) {
        fileCopy.data = _data.copy;
    }

    return fileCopy;
}

#pragma mark - NSObject overrides

- (NSString *)description {
    return [NSString stringWithFormat:@"SDLFile: %@, isPersistent: %@, should overwrite: %@, is static icon: %@, file type: %@", self.name, (self.isPersistent ? @"YES" : @"NO"), (self.overwrite ? @"YES" : @"NO"), (self.isStaticIcon ? @"YES" : @"NO"), self.fileType];
}

- (NSUInteger)hash {
    return self.name.hash ^ self.data.hash;
}

- (BOOL)isEqual:(id)object {
    if (self == object) { return YES; }

    if (![object isKindOfClass:[SDLFile class]]) { return NO; }

    return [self isEqualToFile:(SDLFile *)object];
}

- (BOOL)isEqualToFile:(SDLFile *)file {
    if (!file) { return NO; }

    BOOL haveEqualNames = [self.name isEqualToString:file.name];
    BOOL haveEqualFormats = [self.fileType isEqualToEnum:file.fileType];

    BOOL haveEqualData = NO;
    if (self.data.length == 0 && file.data.length == 0) {
        haveEqualData = [self.fileURL isEqual:file.fileURL];
    } else if (self.data.length > 0 && file.data.length > 0) {
        haveEqualData = [self.data isEqualToData:file.data];
    } else {
        return NO;
    }

    return haveEqualNames && haveEqualData && haveEqualFormats;
}

@end

NS_ASSUME_NONNULL_END