Skip to content

Commit

Permalink
1.Update starred page request interface.
Browse files Browse the repository at this point in the history
2.Update data parsing on starred page.
3.Refactor display logic for starred page.
4.Revise caching logic.
5.Revise navigation logic.
6.Add ‘Locate in Directory’ and ‘Unstar’ Options to Starred Directory Context Menu
7.add Starred Page with Encrypted Folder and File Navigation Logic.
8.Modify Star/Unstar API to V2.1.
9.Starred Files Page Redirection and Permission Issue Fix.

10.photo backup improvements

11.Modify some issues with the starred page.
  • Loading branch information
henry9610 committed Sep 10, 2024
1 parent ed40944 commit f459075
Show file tree
Hide file tree
Showing 34 changed files with 1,180 additions and 212 deletions.
1 change: 1 addition & 0 deletions Pod/Classes/SKFileTypeImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

+ (UIImage *)imageForMimeType:(NSString *)mimeType;
+ (UIImage *)imageForMimeType:(NSString *)mimeType ext:(NSString *)ext;
+ (UIImage*)loadImageWithImgName:(NSString*)imageName;


@end
4 changes: 4 additions & 0 deletions Pod/Classes/SKFileTypeImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ - (NSString*)constructFilenameWithBasename:(NSString*)basename size:(unsigned in
stringByReplacingOccurrencesOfString:@"{extension}" withString:@""];
}

+ (UIImage*)loadImageWithImgName:(NSString*)imageName {
return [[self sharedLoader] loadImageWithName:imageName];
}

- (UIImage*)loadImageWithName:(NSString*)imageName
{
if ([images objectForKey:imageName])
Expand Down
15 changes: 14 additions & 1 deletion Pod/Classes/SeafBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ typedef void (^repo_password_set_block_t)(SeafBase *entry, int ret);
mime:(NSString *)aMime;


@property (copy) NSString *name; // name
@property (copy) NSString *name; //obj name
@property (nonatomic, copy) NSString *repoName;//repo name
@property (readonly, copy) NSString *path; // path in the library
@property (readonly, copy) NSString *repoId; // library id
@property (readonly, copy) NSString *mime; //mime type
Expand All @@ -80,6 +81,10 @@ typedef void (^repo_password_set_block_t)(SeafBase *entry, int ret);

@property (readonly, copy) NSString *shareLink; // shared link

@property (nonatomic, assign) BOOL isDeleted;//2.9.27 mark is deleted

@property (assign, nonatomic) BOOL encrypted;///< Indicates whether the repository is encrypted.

- (BOOL)hasCache; // has local cache
- (BOOL)loadCache; // load local cache
- (void)clearCache; // clear local cache
Expand All @@ -88,6 +93,9 @@ typedef void (^repo_password_set_block_t)(SeafBase *entry, int ret);
- (void)loadContent:(BOOL)force;
- (UIImage *)icon; // icon for this entry

//starred page load content
- (void)loadStarredContent:(BOOL)force;

- (void)generateShareLink:(id<SeafShareDelegate>)dg; // generate shared link


Expand All @@ -99,4 +107,9 @@ typedef void (^repo_password_set_block_t)(SeafBase *entry, int ret);
- (void)downloadComplete:(BOOL)updated;
- (void)downloadFailed:(NSError *)error;

/**
* Sets the starred status of the file.
* @param starred YES to star the file, NO to unstar.
*/
- (void)setStarred:(BOOL)starred;
@end
80 changes: 76 additions & 4 deletions Pod/Classes/SeafBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#import "Utils.h"
#import "Debug.h"

#define REPO_PASSWORD_REFRESH_INTERVAL 300

@implementation NSObject (NSObjectValue)
- (long long)integerValue:(int)defaultValue
{
Expand Down Expand Up @@ -140,9 +142,8 @@ - (NSString *)cacheKey
}


- (void)loadContent:(BOOL)force;
{
BOOL hasCache = [self loadCache];
- (void)loadContent:(BOOL)force {
BOOL hasCache = [self loadCache]; //set true if ooid==nil or has Json cache
@synchronized (self) {
if (hasCache && !force) {
return [self downloadComplete:true];
Expand All @@ -156,7 +157,12 @@ - (void)loadContent:(BOOL)force;

- (BOOL)hasCache
{
return _ooid != nil;
if (_ooid != nil){
return true;
} else {
return false;
}
// return _ooid != nil;
}

- (void)generateShareLink:(id<SeafShareDelegate>)dg password:(NSString *)password expire_days:(NSString *)expire_days {
Expand Down Expand Up @@ -234,4 +240,70 @@ - (void)downloadFailed:(NSError *)error
[self.delegate download:self failed:error];
}

- (void)setStarred:(BOOL)starred
{
[connection setStarred:starred repo:self.repoId path:self.path];
}

- (BOOL)passwordRequiredWithSyncRefresh {
if (self.encrypted) {
if ([connection shouldLocalDecrypt:self.repoId]) {
return [connection getRepoPassword:self.repoId] == nil ? YES : NO;
} else {
NSString *password = [connection getRepoPassword:self.repoId];
if (!password) {
return YES;
} else {
NSTimeInterval cur = [[NSDate date] timeIntervalSince1970];
if (cur - [connection getRepoLastRefreshPasswordTime:self.repoId] > REPO_PASSWORD_REFRESH_INTERVAL) {
__block BOOL result = YES;
__block BOOL wait = YES;
[self setRepoPassword:password block:^(SeafBase *entry, int ret) {
wait = NO;
result = ret == RET_SUCCESS ? NO : YES;
}];
//dispatch_semaphore will block main thread
while (wait) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return result;
} else {
return NO;
}
}
}
} else {
return NO;
}
}

- (void)setRepoPassword:(NSString *)password block:(void(^)(SeafBase *entry, int ret))block
{
if (!self.repoId) {
if (block) block(self, RET_FAILED);
return;
}
NSString *request_str = [NSString stringWithFormat:API_URL"/repos/%@/?op=setpassword", self.repoId];
NSString *formString = [NSString stringWithFormat:@"password=%@", password.escapedPostForm];
__weak typeof(self) wself = self;
[connection sendPost:request_str form:formString
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
__strong typeof(self) sself = wself;
Debug("Set repo %@ password success.", sself.repoId);
[sself->connection saveRepo:sself.repoId password:password];
if (block) block(sself, RET_SUCCESS);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
__strong typeof(self) sself = wself;
Debug("Failed to set repo %@ password: %@, %@", sself.repoId, JSON, error);
int ret = RET_FAILED;
if (JSON != nil) {
NSString *errMsg = [JSON objectForKey:@"error_msg"];
if ([@"Incorrect password" isEqualToString:errMsg]) {
Debug("Repo password incorrect.");
ret = RET_WRONG_PASSWORD;
}
}
if (block) block(sself, ret);
}];
}
@end
6 changes: 6 additions & 0 deletions Pod/Classes/SeafConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SeafCacheProvider.h"
#import "SeafBase.h"
#import "SeafPhotoBackupTool.h"
@class SeafFile;

#define HTTP_ERR_UNAUTHORIZED 401
#define HTTP_ERR_LOGIN_INCORRECT_PASSWORD 400
Expand Down Expand Up @@ -491,5 +492,10 @@ Checks the auto synchronization settings and updates the connection’s synchron
*/
- (void)clearUploadCache;

/**
*
Build starred thumbnail image url string
*/
- (NSString *_Nullable)buildThumbnailImageUrlFromSFile:(SeafFile *_Nullable)sFile;

@end
113 changes: 106 additions & 7 deletions Pod/Classes/SeafConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import "Version.h"
#import "SeafPhotoAsset.h"
#import "SeafRealmManager.h"

#import "SeafFile.h"
enum {
FLAG_LOCAL_DECRYPT = 0x1,
};
Expand Down Expand Up @@ -804,6 +804,14 @@ - (void)loginWithUsername:(NSString *)username password:(NSString *)password
[self loginWithUsername:username password:password otp:nil rememberDevice:false];
}

- (NSString *)buildThumbnailImageUrlFromSFile:(SeafFile *)sFile {
NSString *urlString = [NSString stringWithFormat:@"/api2/repos/%@/thumbnail/?p=%@&size=128", sFile.repoId, sFile.path];

NSString *encodedURL = [self encodeStringToURLFormat:urlString];

return encodedURL;
}

- (NSURLRequest *)buildRequest:(NSString *)url method:(NSString *)method form:(NSString *)form
{
NSString *absoluteUrl = [url hasPrefix:@"http"] ? url : [_address stringByAppendingString:url];
Expand Down Expand Up @@ -912,19 +920,63 @@ - (void)loadRepos:(id<SeafDentryDelegate>)degt
[_rootFolder loadContent:NO];
}

//befor 2.9.27 old api for handle data
//- (void)handleStarredData:(id)JSON
//{
// NSMutableSet *stars = [NSMutableSet set];
// for (NSDictionary *info in JSON) {
// [stars addObject:[NSString stringWithFormat:@"%@-%@", [info objectForKey:@"repo"], [info objectForKey:@"path"]]];
// }
// _starredFiles = stars;
//}

- (void)handleStarredData:(id)JSON
{
if (![JSON isKindOfClass:[NSDictionary class]]) {
Debug(@"Expected a dictionary with a 'starred_item_list' key");
return;
}

NSArray *starredItems = [JSON objectForKey:@"starred_item_list"];
if (![starredItems isKindOfClass:[NSArray class]]) {
Debug(@"Expected 'starred_item_list' to be an array");
return;
}

NSMutableSet *stars = [NSMutableSet set];
for (NSDictionary *info in JSON) {
[stars addObject:[NSString stringWithFormat:@"%@-%@", [info objectForKey:@"repo"], [info objectForKey:@"path"]]];
for (NSDictionary *info in starredItems) {
[stars addObject:[NSString stringWithFormat:@"%@-%@", [info objectForKey:@"repo_id"], [info objectForKey:@"path"]]];
}
_starredFiles = stars;
}

//befor 2.9.27 old api for get starred files
//- (void)getStarredFiles:(void (^)(NSHTTPURLResponse *response, id JSON))success
// failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
//{
// [self sendRequest:API_URL"/starredfiles/"
// success:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// @synchronized(self) {
// Debug("Succeeded to get starred files ...\n");
// [self handleStarredData:JSON];
// NSData *data = [Utils JSONEncode:JSON];
// [self setValue:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:KEY_STARREDFILES entityName:ENTITY_OBJECT];
// if (success)
// success (response, JSON);
// }
// }
// failure:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
// if (failure)
// failure (response, error);
// }];
//}

- (void)getStarredFiles:(void (^)(NSHTTPURLResponse *response, id JSON))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
{
[self sendRequest:API_URL"/starredfiles/"
[self sendRequest:API_URL_V21"/starred-items/"
success:
^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
@synchronized(self) {
Expand Down Expand Up @@ -976,8 +1028,9 @@ - (BOOL)setStarred:(BOOL)starred repo:(NSString *)repo path:(NSString *)path
NSString *key = [NSString stringWithFormat:@"%@-%@", repo, path];
if (starred) {
[_starredFiles addObject:key];
NSString *form = [NSString stringWithFormat:@"repo_id=%@&p=%@", repo, [path escapedUrl]];
NSString *url = [NSString stringWithFormat:API_URL"/starredfiles/"];
NSString *form = [NSString stringWithFormat:@"repo_id=%@&path=%@", repo, [path escapedUrl]];
NSString *url = [NSString stringWithFormat:API_URL_V21"/starred-items/"];

[self sendPost:url form:form
success:
^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
Expand All @@ -989,7 +1042,7 @@ - (BOOL)setStarred:(BOOL)starred repo:(NSString *)repo path:(NSString *)path
}];
} else {
[_starredFiles removeObject:key];
NSString *url = [NSString stringWithFormat:API_URL"/starredfiles/?repo_id=%@&p=%@", repo, path.escapedUrl];
NSString *url = [NSString stringWithFormat:API_URL_V21"/starred-items/?repo_id=%@&path=%@", repo, path.escapedUrl];
[self sendDelete:url
success:
^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
Expand All @@ -1004,6 +1057,42 @@ - (BOOL)setStarred:(BOOL)starred repo:(NSString *)repo path:(NSString *)path
return YES;
}

//before 2.9.27 old api for set starred file
//- (BOOL)setStarred:(BOOL)starred repo:(NSString *)repo path:(NSString *)path
//{
// NSString *key = [NSString stringWithFormat:@"%@-%@", repo, path];
// if (starred) {
// [_starredFiles addObject:key];
// NSString *form = [NSString stringWithFormat:@"repo_id=%@&p=%@", repo, [path escapedUrl]];
// NSString *url = [NSString stringWithFormat:API_URL"/starredfiles/"];
//// NSString *url = [NSString stringWithFormat:API_URL_V21"/starredfiles/"];
//
// [self sendPost:url form:form
// success:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Debug("Succeeded to star file %@, %@\n", repo, path);
// }
// failure:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
// Warning("Failed to star file %@, %@\n", repo, path);
// }];
// } else {
// [_starredFiles removeObject:key];
// NSString *url = [NSString stringWithFormat:API_URL"/starredfiles/?repo_id=%@&p=%@", repo, path.escapedUrl];
// [self sendDelete:url
// success:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Debug("Succeeded to unstar file %@, %@\n", repo, path);
// }
// failure:
// ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
// Warning("Failed to unstar file %@, %@\n", repo, path);
// }];
// }
//
// return YES;
//}

- (SeafRepo *)getRepo:(NSString *)repo
{
return [self.rootFolder getRepo:repo];
Expand Down Expand Up @@ -1497,6 +1586,16 @@ - (void)updateKeyValuePairs:(NSNotification*)notification {
}
}

- (NSString *)encodeStringToURLFormat:(NSString *)string {
// Create a character set that includes all characters allowed in a URL query
NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];

// Encode the string using the specified character set
NSString *encodedString = [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

return encodedString;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];
}
Expand Down
13 changes: 12 additions & 1 deletion Pod/Classes/SeafDataTaskManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ - (id)init
_finishBlock = nil;
[self startTimer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheCleared:) name:@"clearCache" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
Expand Down Expand Up @@ -340,7 +341,11 @@ - (void)startLastTimeUnfinshTaskWithConnection:(SeafConnection *)conn {
NSDictionary *downloadTasks = [SeafStorage.sharedObject objectForKey:downloadKey];
if (downloadTasks.allValues.count > 0) {
for (NSDictionary *dict in downloadTasks.allValues) {
SeafFile *file = [[SeafFile alloc] initWithConnection:conn oid:[dict objectForKey:@"oid"] repoId:[dict objectForKey:@"repoId"] name:[dict objectForKey:@"name"] path:[dict objectForKey:@"path"] mtime:[[dict objectForKey:@"mtime"] longLongValue] size:[[dict objectForKey:@"size"] longLongValue]];
NSNumber *mtimeNumber = [dict objectForKey:@"mtime"];

NSString *oid = [Utils getNewOidFromMtime:[mtimeNumber longLongValue] repoId:[dict objectForKey:@"repoId"] path:[dict objectForKey:@"path"]];

SeafFile *file = [[SeafFile alloc] initWithConnection:conn oid:oid repoId:[dict objectForKey:@"repoId"] name:[dict objectForKey:@"name"] path:[dict objectForKey:@"path"] mtime:[[dict objectForKey:@"mtime"] longLongValue] size:[[dict objectForKey:@"size"] longLongValue]];
[self addFileDownloadTask:file];
}
}
Expand Down Expand Up @@ -420,6 +425,12 @@ - (void)removeAccountUploadTaskFromStorage:(NSString *)accountIdentifier {
[SeafStorage.sharedObject removeObjectForKey:key];
}

- (void)applicationWillEnterForeground:(NSNotification *)notif {
[self.taskTimer invalidate];
self.taskTimer = nil;
self.taskTimer = [NSTimer scheduledTimerWithTimeInterval:1*60 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Expand Down
Loading

0 comments on commit f459075

Please sign in to comment.