Esempio n. 1
0
        public async Task <bool> Move(string oldPath, string newPath)
        {
            var client = ClientService.GetClient();

            if (client == null)
            {
                return(false);
            }

            var success = false;

            try
            {
                success = await client.Move(oldPath, newPath);
            }
            catch (ResponseError e)
            {
                if (e.StatusCode != "400") // ProtocolError
                {
                    ResponseErrorHandlerService.HandleException(e);
                }
            }

            if (success)
            {
                await StartDirectoryListing();
            }
            return(success);
        }
Esempio n. 2
0
        public async Task <bool> Rename(string oldName, string newName)
        {
            var client = ClientService.GetClient();

            if (client == null)
            {
                return(false);
            }

            var path = PathStack.Count > 0 ? PathStack[PathStack.Count - 1].ResourceInfo.Path : "";

            var success = false;

            try
            {
                success = await client.Move(path + "/" + oldName, path + "/" + newName);
            }
            catch (ResponseError e)
            {
                if (e.StatusCode != "400") // ProtocolError
                {
                    ResponseErrorHandlerService.HandleException(e);
                }
            }

            if (success)
            {
                await StartDirectoryListing();
            }
            return(success);
        }
Esempio n. 3
0
        private async Task <bool> UploadFile(StorageFile localFile, string path)
        {
            bool result = false;
            var  _cts   = new CancellationTokenSource();

            CachedFileManager.DeferUpdates(localFile);
            try
            {
                var properties = await localFile.GetBasicPropertiesAsync();

                long BytesTotal = (long)properties.Size;

                using (var stream = await localFile.OpenAsync(FileAccessMode.Read))
                {
                    var targetStream = stream.AsStreamForRead();

                    IProgress <WebDavProgress> progress = new Progress <WebDavProgress>(ProgressHandler);
                    result = await client.Upload(path, targetStream, localFile.ContentType, progress, _cts.Token);
                }
            }
            catch (ResponseError e2)
            {
                ResponseErrorHandlerService.HandleException(e2);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            await CachedFileManager.CompleteUpdatesAsync(localFile);

            return(result);
        }
        public async Task StartDirectoryListing()
        {
            var client = await ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            _continueListing = true;

            var path = PathStack.Count > 0 ? PathStack[PathStack.Count - 1].ResourceInfo.Path : "/";
            List <ResourceInfo> list = null;

            try
            {
                list = await client.List(path);
            }
            catch (ResponseError e)
            {
                ResponseErrorHandlerService.HandleException(e);
            }

            FilesAndFolders.Clear();
            Folders.Clear();
            if (list != null)
            {
                foreach (var item in list)
                {
                    FilesAndFolders.Add(new FileOrFolder(item));
                    if (item.IsDirectory())
                    {
                        Folders.Add(new FileOrFolder(item));
                    }
                }
            }

            switch (SettingsService.Instance.LocalSettings.PreviewImageDownloadMode)
            {
            case PreviewImageDownloadMode.Always:
                DownloadPreviewImages();
                break;

            case PreviewImageDownloadMode.WiFiOnly:
                var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                // connectionProfile can be null (e.g. airplane mode)
                if (connectionProfile != null && connectionProfile.IsWlanConnectionProfile)
                {
                    DownloadPreviewImages();
                }
                break;

            case PreviewImageDownloadMode.Never:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private async void DownloadPreviewImages()
        {
            var client = await ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            foreach (var currentFile in FilesAndFolders.ToArray())
            {
                if (!_continueListing)
                {
                    break;
                }

                try
                {
                    Stream stream = null;
                    try
                    {
                        stream = await client.GetThumbnail(currentFile, 120, 120);
                    }
                    catch (ResponseError e)
                    {
                        ResponseErrorHandlerService.HandleException(e);
                    }

                    if (stream == null)
                    {
                        continue;
                    }
                    var bitmap = new BitmapImage();
                    using (var memStream = new MemoryStream())
                    {
                        await stream.CopyToAsync(memStream);

                        memStream.Position = 0;
                        bitmap.SetSource(memStream.AsRandomAccessStream());
                    }
                    currentFile.Thumbnail = bitmap;
                }
                catch (ResponseError)
                {
                    currentFile.Thumbnail = new BitmapImage
                    {
                        UriSource = new Uri("ms-appx:///Assets/Images/ThumbnailNotFound.png")
                    };
                }
            }
        }
        public async Task StartDirectoryListing(ResourceInfo resourceInfoToExclude, String viewName = null)
        {
            var client = await ClientService.GetClient();

            if (client == null || IsSelecting)
            {
                return;
            }

            _continueListing = true;

            if (PathStack.Count == 0)
            {
                PathStack.Add(new PathInfo
                {
                    ResourceInfo = new ResourceInfo()
                    {
                        Name = "Nextcloud",
                        Path = "/"
                    },
                    IsRoot = true
                });
            }

            var path = PathStack.Count > 0 ? PathStack[PathStack.Count - 1].ResourceInfo.Path : "/";
            List <ResourceInfo> list = null;

            try
            {
                if (viewName == "sharesIn" | viewName == "sharesOut" | viewName == "sharesLink")
                {
                    PathStack.Clear();
                    list = await client.GetSharesView(viewName);
                }
                else if (viewName == "favorites")
                {
                    PathStack.Clear();
                    list = await client.GetFavorites();
                }
                else
                {
                    list = await client.List(path);
                }
            }
            catch (ResponseError e)
            {
                ResponseErrorHandlerService.HandleException(e);
            }

            FilesAndFolders.Clear();
            Folders.Clear();

            if (list != null)
            {
                foreach (var item in list)
                {
                    if (resourceInfoToExclude != null && item == resourceInfoToExclude)
                    {
                        continue;
                    }

                    FilesAndFolders.Add(new FileOrFolder(item));

                    if (!item.IsDirectory)
                    {
                        continue;
                    }
                    if (RemoveResourceInfos != null)
                    {
                        var index = RemoveResourceInfos.FindIndex(res => res.Path.Equals(item.Path, StringComparison.Ordinal));
                        if (index == -1)
                        {
                            Folders.Add(new FileOrFolder(item));
                        }
                    }
                    else
                    {
                        Folders.Add(new FileOrFolder(item));
                    }
                }
            }

            switch (SettingsService.Instance.LocalSettings.PreviewImageDownloadMode)
            {
            case PreviewImageDownloadMode.Always:
                DownloadPreviewImages();
                break;

            case PreviewImageDownloadMode.WiFiOnly:
                var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                // connectionProfile can be null (e.g. airplane mode)
                if (connectionProfile != null && connectionProfile.IsWlanConnectionProfile)
                {
                    DownloadPreviewImages();
                }
                break;

            case PreviewImageDownloadMode.Never:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            SortList();
        }
Esempio n. 7
0
        /// <summary>
        /// Folder Synchronization
        /// </summary>
        /// <param name="resourceInfo">webdav Resource to sync</param>
        /// <param name="folder">Target folder</param>
        private async Task <int> SyncFolder(ResourceInfo info, StorageFolder folder)
        {
            SyncInfoDetail sid = SyncDbUtils.GetSyncInfoDetail(info, folderSyncInfo);

            sid.Error = null;
            int changesCount = 0;

            try
            {
                Debug.WriteLine("Sync folder " + info.Path + ":" + folder.Path);
                IReadOnlyList <StorageFile> localFiles = await folder.GetFilesAsync();

                IReadOnlyList <StorageFolder> localFolders = await folder.GetFoldersAsync();

                List <ResourceInfo> list = null;
                try
                {
                    list = await client.List(info.Path);
                }
                catch (ResponseError e)
                {
                    ResponseErrorHandlerService.HandleException(e);
                }
                //List<Task> syncTasks = new List<Task>();
                List <IStorageItem> synced = new List <IStorageItem>();
                if (list != null && list.Count > 0)
                {
                    foreach (ResourceInfo subInfo in list)
                    {
                        if (subInfo.IsDirectory)
                        {
                            IEnumerable <StorageFolder> localFoldersWithName = localFolders.Where(f => f.Name.Equals(subInfo.Name));
                            StorageFolder subFolder = localFoldersWithName.FirstOrDefault();
                            // Can localFoldersWithName be null?
                            if (subFolder == null)
                            {
                                var subSid = SyncDbUtils.GetSyncInfoDetail(subInfo, folderSyncInfo);
                                if (subSid != null)
                                {
                                    Debug.WriteLine("Sync folder (delete remotely) " + subInfo.Path);
                                    if (await client.Delete(subInfo.Path))
                                    {
                                        SyncDbUtils.DeleteSyncInfoDetail(subSid, true);
                                    }
                                    else
                                    {
                                        sid.Error = "Deletion of " + subInfo.Path + " on nextcloud failed.";
                                        // Error could be overridden by other errors
                                    }
                                }
                                else
                                {
                                    // Create sid and local folder
                                    Debug.WriteLine("Sync folder (create locally) " + subInfo.Path);
                                    subFolder = await folder.CreateFolderAsync(subInfo.Name);

                                    SyncInfoDetail syncInfoDetail = new SyncInfoDetail(folderSyncInfo)
                                    {
                                        Path     = subInfo.Path,
                                        FilePath = subFolder.Path
                                    };

                                    SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                                    changesCount = changesCount + await SyncFolder(subInfo, subFolder);

                                    // syncTasks.Add(SyncFolder(subInfo, subFolder));
                                }
                            }
                            else
                            {
                                var subSid = SyncDbUtils.GetSyncInfoDetail(subInfo, folderSyncInfo);
                                if (subSid == null)
                                {
                                    // Both new
                                    Debug.WriteLine("Sync folder (create both) " + subInfo.Path);

                                    SyncInfoDetail syncInfoDetail = new SyncInfoDetail(folderSyncInfo)
                                    {
                                        Path     = subInfo.Path,
                                        FilePath = subFolder.Path
                                    };

                                    SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                                }
                                synced.Add(subFolder);
                                changesCount = changesCount + await SyncFolder(subInfo, subFolder);

                                // syncTasks.Add(SyncFolder(subInfo, subFolder));
                            }
                        }
                        else
                        {
                            IEnumerable <StorageFile> localFilessWithName = localFiles.Where(f => f.Name.Equals(subInfo.Name));
                            // Can localFilessWithName be null?
                            StorageFile subFile = localFilessWithName.FirstOrDefault();
                            if (subFile != null)
                            {
                                synced.Add(subFile);
                            }
                            changesCount = changesCount + await SyncFile(subInfo, subFile, info, folder);

                            //syncTasks.Add(SyncFile(subInfo, subFile, info, folder));
                        }
                    }
                }
                foreach (StorageFile file in localFiles)
                {
                    if (!synced.Contains(file))
                    {
                        changesCount = changesCount + await SyncFile(null, file, info, folder);

                        //syncTasks.Add(SyncFile(null, file, info, folder));
                    }
                }
                foreach (StorageFolder localFolder in localFolders)
                {
                    if (!synced.Contains(localFolder))
                    {
                        var subSid = SyncDbUtils.GetSyncInfoDetail(localFolder, folderSyncInfo);
                        if (subSid != null)
                        {
                            // Delete all sids and local folder
                            Debug.WriteLine("Sync folder (delete locally) " + localFolder.Path);
                            await localFolder.DeleteAsync();

                            SyncDbUtils.DeleteSyncInfoDetail(subSid, true);
                        }
                        else
                        {
                            // Create sid and remotefolder
                            string newPath = info.Path + localFolder.Name;
                            Debug.WriteLine("Sync folder (create remotely) " + newPath);

                            if (await client.CreateDirectory(newPath))
                            {
                                ResourceInfo subInfo = await client.GetResourceInfo(info.Path, localFolder.Name);

                                SyncInfoDetail syncInfoDetail = new SyncInfoDetail(folderSyncInfo)
                                {
                                    Path     = subInfo.Path,
                                    FilePath = localFolder.Path
                                };

                                SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                                changesCount = changesCount + await SyncFolder(subInfo, localFolder);

                                //syncTasks.Add(SyncFolder(subInfo, localFolder));
                            }
                            else
                            {
                                sid.Error = "Could not create directory on nextcloud: " + newPath;
                            }
                        }
                    }
                }
                //Task.WaitAll(syncTasks.ToArray());
            }
            catch (Exception e)
            {
                sid.Error = e.Message;
            }
            sidList.Add(sid);
            SyncDbUtils.SaveSyncInfoDetail(sid);
            SyncDbUtils.SaveSyncHistory(sid);
            return(changesCount);
        }
        /// <summary>
        /// Folder Synchronization
        /// </summary>
        /// <param name="resourceInfo">webdav Resource to sync</param>
        /// <param name="folder">Target folder</param>
        private async Task <int> SyncFolder(ResourceInfo resourceInfo, StorageFolder folder)
        {
            var sid = SyncDbUtils.GetSyncInfoDetail(resourceInfo, _folderSyncInfo);

            sid.Error = null;
            var changesCount = 0;

            try
            {
                Debug.WriteLine("Sync folder " + resourceInfo.Path + ":" + folder.Path);
                var localFiles = await folder.GetFilesAsync();

                var localFolders = await folder.GetFoldersAsync();

                List <ResourceInfo> list = null;
                try
                {
                    list = await _client.List(resourceInfo.Path);
                }
                catch (ResponseError e)
                {
                    ResponseErrorHandlerService.HandleException(e);
                }

                var synced = new List <IStorageItem>();

                if (list != null && list.Count > 0)
                {
                    foreach (var subInfo in list)
                    {
                        if (subInfo.IsDirectory)
                        {
                            var localFoldersWithName = localFolders.Where(f => f.Name.Equals(subInfo.Name));
                            var subFolder            = localFoldersWithName.FirstOrDefault();
                            // Can localFoldersWithName be null?
                            if (subFolder == null)
                            {
                                var subSid = SyncDbUtils.GetSyncInfoDetail(subInfo, _folderSyncInfo);
                                if (subSid != null)
                                {
                                    Debug.WriteLine("Sync folder (delete remotely) " + subInfo.Path);
                                    if (await _client.Delete(subInfo.Path))
                                    {
                                        SyncDbUtils.DeleteSyncInfoDetail(subSid, true);
                                    }
                                    else
                                    {
                                        sid.Error = string.Format(_resourceLoader.GetString(ResourceConstants.SyncService_Error_DeleteFolderRemotely), subInfo.Path);
                                        // Error could be overridden by other errors
                                    }
                                }
                                else
                                {
                                    // Create sid and local folder
                                    Debug.WriteLine("Sync folder (create locally) " + subInfo.Path);
                                    subFolder = await folder.CreateFolderAsync(subInfo.Name);

                                    var syncInfoDetail = new SyncInfoDetail(_folderSyncInfo)
                                    {
                                        Path     = subInfo.Path,
                                        FilePath = subFolder.Path
                                    };

                                    SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                                    changesCount = changesCount + await SyncFolder(subInfo, subFolder);

                                    // syncTasks.Add(SyncFolder(subInfo, subFolder));
                                }
                            }
                            else
                            {
                                var subSid = SyncDbUtils.GetSyncInfoDetail(subInfo, _folderSyncInfo);
                                if (subSid == null)
                                {
                                    // Both new
                                    Debug.WriteLine("Sync folder (create both) " + subInfo.Path);

                                    var syncInfoDetail = new SyncInfoDetail(_folderSyncInfo)
                                    {
                                        Path     = subInfo.Path,
                                        FilePath = subFolder.Path
                                    };

                                    SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                                }
                                synced.Add(subFolder);
                                changesCount = changesCount + await SyncFolder(subInfo, subFolder);

                                // syncTasks.Add(SyncFolder(subInfo, subFolder));
                            }
                        }
                        else
                        {
                            var localFilessWithName = localFiles.Where(f => f.Name.Equals(subInfo.Name));
                            // Can localFilessWithName be null?
                            var subFile = localFilessWithName.FirstOrDefault();
                            if (subFile != null)
                            {
                                synced.Add(subFile);
                            }
                            changesCount = changesCount + await SyncFile(subInfo, subFile, resourceInfo, folder);

                            //syncTasks.Add(SyncFile(subInfo, subFile, info, folder));
                        }
                    }
                }
                foreach (var file in localFiles)
                {
                    if (!synced.Contains(file))
                    {
                        changesCount = changesCount + await SyncFile(null, file, resourceInfo, folder);

                        //syncTasks.Add(SyncFile(null, file, info, folder));
                    }
                }
                foreach (var localFolder in localFolders)
                {
                    if (synced.Contains(localFolder))
                    {
                        continue;
                    }
                    var subSid = SyncDbUtils.GetSyncInfoDetail(localFolder, _folderSyncInfo);
                    if (subSid != null)
                    {
                        // Delete all sids and local folder
                        Debug.WriteLine("Sync folder (delete locally) " + localFolder.Path);
                        await localFolder.DeleteAsync();

                        SyncDbUtils.DeleteSyncInfoDetail(subSid, true);
                    }
                    else
                    {
                        // Create sid and remotefolder
                        var newPath = resourceInfo.Path + localFolder.Name;
                        Debug.WriteLine("Sync folder (create remotely) " + newPath);

                        if (await _client.CreateDirectory(newPath))
                        {
                            var subInfo = await _client.GetResourceInfo(resourceInfo.Path, localFolder.Name);

                            var syncInfoDetail = new SyncInfoDetail(_folderSyncInfo)
                            {
                                Path     = subInfo.Path,
                                FilePath = localFolder.Path
                            };

                            SyncDbUtils.SaveSyncInfoDetail(syncInfoDetail);
                            changesCount = changesCount + await SyncFolder(subInfo, localFolder);

                            //syncTasks.Add(SyncFolder(subInfo, localFolder));
                        }
                        else
                        {
                            sid.Error = string.Format(_resourceLoader.GetString(ResourceConstants.SyncService_Error_CreateFolderRemotely), newPath);
                        }
                    }
                }
                //Task.WaitAll(syncTasks.ToArray());
            }
            catch (Exception e)
            {
                sid.Error = string.Format(_resourceLoader.GetString("UnexpectedException"), e.Message);
            }
            _sidList.Add(sid);
            SyncDbUtils.SaveSyncInfoDetail(sid);
            SyncDbUtils.SaveSyncHistory(sid);
            return(changesCount);
        }