Felix Paul Kühne преди 10 години
родител
ревизия
fd2cf1bb6b
променени са 3 файла, в които са добавени 41 реда и са изтрити 11 реда
  1. 37 5
      Sources/VLCBoxController.m
  2. 3 4
      Sources/VLCBoxTableViewController.m
  3. 1 2
      Sources/VLCCloudStorageTableViewCell.m

+ 37 - 5
Sources/VLCBoxController.m

@@ -15,7 +15,7 @@
 #import "VLCAppDelegate.h"
 #import <SSKeychain/SSKeychain.h>
 
-@interface VLCBoxController ()
+@interface VLCBoxController () <NSURLConnectionDataDelegate>
 {
     BoxCollection *_fileList;
     BoxAPIJSONOperation *_operation;
@@ -148,10 +148,42 @@
 
 - (void)streamFile:(BoxFile *)file
 {
-//    VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
-//    NSString *token = [BoxSDK sharedSDK].OAuth2Session.accessToken;
-//    NSString *downloadString = [@"https://api.box.com/2.0/files/"stringByAppendingString:[NSString stringWithFormat:@"%@&access_token=%@",file.modelID, token]];
-//    [appDelegate openMovieFromURL:[NSURL URLWithString:downloadString]];
+    /* the Box API requires us to set an HTTP header to get the actual URL:
+     * curl -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer ACCESS_TOKEN"
+     *
+     * ... however, libvlc does not support setting custom HTTP headers, so we are resolving the redirect ourselves with a NSURLConnection
+     * and pass the final location to libvlc, which does not require a custom HTTP header */
+
+    NSURL *baseURL = [[[BoxSDK sharedSDK] filesManager] URLWithResource:@"files"
+                                        ID:file.modelID
+                               subresource:@"content"
+                                     subID:nil];
+
+    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:baseURL
+                                                              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
+                                                          timeoutInterval:60];
+
+    [urlRequest setValue:[NSString stringWithFormat:@"Bearer %@", [BoxSDK sharedSDK].OAuth2Session.accessToken] forHTTPHeaderField:@"Authorization"];
+
+    NSURLConnection *theTestConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
+    [theTestConnection start];
+}
+
+- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
+{
+    if (response != nil) {
+        /* we have 1 redirect from the original URL, so as soon as we'd do that,
+         * we grab the URL and cancel the connection */
+        NSURL *theActualURL = request.URL;
+
+        [connection cancel];
+
+        /* now ask VLC to stream the URL we were just passed */
+        VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
+        [appDelegate openMovieFromURL:theActualURL];
+    }
+
+    return request;
 }
 
 - (void)_triggerNextDownload

+ 3 - 4
Sources/VLCBoxTableViewController.m

@@ -118,10 +118,9 @@
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     _selectedFile = _boxController.currentListFiles[indexPath.row];
-    if (![_selectedFile.type isEqualToString:@"folder"]) {
-        //[_boxController streamFile:(BoxFile *)_selectedFile];
-        [_boxController downloadFileToDocumentFolder:_selectedFile];
-    } else {
+    if (![_selectedFile.type isEqualToString:@"folder"])
+        [_boxController streamFile:(BoxFile *)_selectedFile];
+    else {
         /* dive into subdirectory */
         if (![self.currentPath isEqualToString:@""])
             self.currentPath = [self.currentPath stringByAppendingString:@"/"];

+ 1 - 2
Sources/VLCCloudStorageTableViewCell.m

@@ -129,6 +129,7 @@
             self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
             self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
             self.folderTitleLabel.hidden = YES;
+            self.downloadButton.hidden = NO;
         }
         //TODO: correct thumbnails
 //        if (_boxFile.modelID != nil) {
@@ -176,8 +177,6 @@
                 self.thumbnailView.image = [UIImage imageNamed:@"blank"];
         }
     }
-    //we don't have streaming for box yet
-    self.downloadButton.hidden = _boxFile != nil;
 
     [self setNeedsDisplay];
 }