Skip to content

Commit

Permalink
Untested(!) fixes for OneDrive
Browse files Browse the repository at this point in the history
  • Loading branch information
softworkz committed Nov 24, 2016
1 parent a0e036a commit fea8f34
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
66 changes: 41 additions & 25 deletions OneDrive/OneDriveServerSyncProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Interfaces.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Net;
Expand Down Expand Up @@ -105,23 +105,28 @@ public async Task<Stream> GetFile(string id, SyncTarget target, IProgress<double
});
}

public async Task<QueryResult<FileMetadata>> GetFiles(FileQuery query, SyncTarget target, CancellationToken cancellationToken)
public async Task<QueryResult<FileSystemMetadata>> GetFiles(string[] pathParts, SyncTarget target, CancellationToken cancellationToken)
{
try
{
return await TryGetFiles(query, target, cancellationToken);
return await TryGetFiles(pathParts, target, cancellationToken);
}
catch (HttpException ex)
{
if (ex.StatusCode == HttpStatusCode.NotFound)
{
return new QueryResult<FileMetadata>();
return new QueryResult<FileSystemMetadata>();
}

throw;
}
}

public Task<QueryResult<FileSystemMetadata>> GetFiles(SyncTarget target, CancellationToken cancellationToken)
{
return GetFiles(null, target, cancellationToken);
}

public async Task<SyncedFileInfo> GetSyncedFileInfo(string id, SyncTarget target, CancellationToken cancellationToken)
{
_logger.Debug("Getting synced file info for {0} from {1}", id, target.Name);
Expand Down Expand Up @@ -215,74 +220,85 @@ private static async Task<byte[]> FillBuffer(Stream stream, CancellationToken ca
return buffer;
}

private async Task<QueryResult<FileMetadata>> TryGetFiles(FileQuery query, SyncTarget target, CancellationToken cancellationToken)
private async Task<QueryResult<FileSystemMetadata>> TryGetFiles(string[] pathParts, SyncTarget target, CancellationToken cancellationToken)
{
var oneDriveCredentials = CreateOneDriveCredentials(target);

if (!string.IsNullOrEmpty(query.Id))
////if (!string.IsNullOrEmpty(query.Id))
////{
//// return await GetFileById(query.Id, oneDriveCredentials, cancellationToken);
////}

var fullPath = FindPathFromFileQuery(pathParts, target);

if (fullPath != null && fullPath.Length > 0)
{
return await GetFileById(query.Id, oneDriveCredentials, cancellationToken);
return await GetFileByPath(fullPath, oneDriveCredentials, cancellationToken);
}

if (query.FullPath != null && query.FullPath.Length > 0)
return await GetAllFiles(oneDriveCredentials, cancellationToken);
}

private string FindPathFromFileQuery(string[] parts, SyncTarget target)
{
if (parts != null && parts.Length > 0)
{
var path = GetFullPath(query.FullPath);
return await GetFileByPath(path, oneDriveCredentials, cancellationToken);
return GetFullPath(parts);
}

return await GetAllFiles(oneDriveCredentials, cancellationToken);
return string.Empty;
}

private async Task<QueryResult<FileMetadata>> GetFileById(string id, OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
private async Task<QueryResult<FileSystemMetadata>> GetFileById(string id, OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
{
var viewChangeResult = await _oneDriveApi.ViewChangeById(id, oneDriveCredentials, cancellationToken);
var viewChanges = viewChangeResult.value.Select(CreateFileMetadata).ToArray();
var viewChanges = viewChangeResult.value.Select(CreateFileSystemMetadata).ToArray();

return new QueryResult<FileMetadata>
return new QueryResult<FileSystemMetadata>
{
Items = viewChanges,
TotalRecordCount = viewChanges.Length
};
}

private async Task<QueryResult<FileMetadata>> GetFileByPath(string path, OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
private async Task<QueryResult<FileSystemMetadata>> GetFileByPath(string path, OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
{
var viewChangeResult = await _oneDriveApi.ViewChangeByPath(path, oneDriveCredentials, cancellationToken);
var viewChanges = viewChangeResult.value.Select(CreateFileMetadata).ToArray();
var viewChanges = viewChangeResult.value.Select(CreateFileSystemMetadata).ToArray();

return new QueryResult<FileMetadata>
return new QueryResult<FileSystemMetadata>
{
Items = viewChanges,
TotalRecordCount = viewChanges.Length
};
}

private async Task<QueryResult<FileMetadata>> GetAllFiles(OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
private async Task<QueryResult<FileSystemMetadata>> GetAllFiles(OneDriveCredentials oneDriveCredentials, CancellationToken cancellationToken)
{
var viewChangeResult = new ViewChangesResult { HasMoreChanges = true };
var files = new List<FileMetadata>();
var files = new List<FileSystemMetadata>();

while (viewChangeResult.HasMoreChanges)
{
viewChangeResult = await _oneDriveApi.ViewChanges(viewChangeResult.Token, oneDriveCredentials, cancellationToken);
var newFiles = viewChangeResult.value.Select(CreateFileMetadata);
var newFiles = viewChangeResult.value.Select(CreateFileSystemMetadata);
files.AddRange(newFiles);
}

return new QueryResult<FileMetadata>
return new QueryResult<FileSystemMetadata>
{
Items = files.ToArray(),
TotalRecordCount = files.Count
};
}

private FileMetadata CreateFileMetadata(ViewChange viewChange)
private FileSystemMetadata CreateFileSystemMetadata(ViewChange viewChange)
{
return new FileMetadata
return new FileSystemMetadata
{
Id = viewChange.id,
FullName = viewChange.id,
Name = viewChange.name,
IsFolder = viewChange.folder != null
IsDirectory = viewChange.folder != null
};
}

Expand Down
2 changes: 1 addition & 1 deletion OneDrive/RestServices/AddSyncTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using ServiceStack;
using MediaBrowser.Model.Services;

namespace OneDrive.RestServices
{
Expand Down
2 changes: 1 addition & 1 deletion OneDrive/RestServices/DeleteSyncTarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ServiceStack;
using MediaBrowser.Model.Services;

namespace OneDrive.RestServices
{
Expand Down
4 changes: 2 additions & 2 deletions OneDrive/RestServices/GetSyncTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using OneDrive.Configuration;
using ServiceStack;
using MediaBrowser.Model.Services;
using OneDrive.Configuration;

namespace OneDrive.RestServices
{
Expand Down
3 changes: 2 additions & 1 deletion OneDrive/RestServices/OneDriveRestfulService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using MediaBrowser.Controller.Net;
using OneDrive.Api;
using OneDrive.Configuration;
using MediaBrowser.Model.Services;

namespace OneDrive.RestServices
{
[Authenticated]
public class OneDriveRestfulService : IRestfulService
public class OneDriveRestfulService : IService
{
private readonly IConfigurationRetriever _configurationRetriever;
private readonly ILiveAuthenticationApi _liveAuthenticationApi;
Expand Down

0 comments on commit fea8f34

Please sign in to comment.