Bladeren bron

云文档 - 上传文件到指定云盘指定的文件夹里,并更新文件列表

chenrongqian 2 jaren geleden
bovenliggende
commit
3767c8715a

+ 0 - 1
PDF Office/Model/CloudDrive/CloudDriveItem.cs

@@ -16,7 +16,6 @@ namespace PDF_Office.Model.CloudDrive
         OneDrive
     }
 
-
     public class CloudBoxItem
     {
         public CloudType CloudDiskType { get; set; }

+ 3 - 1
PDF Office/Model/CloudDrive/CloudFiles.cs

@@ -103,7 +103,7 @@ namespace PDF_Office.Model.CloudDrive
     /// </summary>
     public class DropbBoxFiles : FilesBaseItem
     {
-
+        public string PathDisplay { get; set; }
     }
     public class DropbBoxFolder : FolderBaseItem
     {
@@ -116,6 +116,8 @@ namespace PDF_Office.Model.CloudDrive
     /// </summary>
     public class FileOperation
     {
+        public string SourceFileName { get; set; }
+        public string SourceFile { get; set; }
         public UserBaseItem User { get; set; }
         public FilesBaseItem DoFile { get; set; }
         public FileOperation(UserBaseItem user, FilesBaseItem doFile)

+ 0 - 4
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveContentViewModel.cs

@@ -11,8 +11,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 {
     public class CloudDriveContentViewModel : BindableBase
     {
-
-
         public DelegateCommand<CloudBoxItem> CheckDriveCommand { get; set; }
         public DelegateCommand<CloudBoxItem> CheckDriveLoginUserCommand { get; set; }
 
@@ -35,8 +33,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         public async void CheckDrive(CloudBoxItem cloudDriveItem)
         {
-            //IshowContentHandler?.Invoke(null, false);
-            //return;
             bool result = false;
             result = await Manager.LoginUser(cloudDriveItem.CloudDiskType);
             IshowContentHandler?.Invoke(null, !result);

+ 25 - 20
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveManager.cs

@@ -180,34 +180,39 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
             return result;
         }
 
-        /// <summary>
-        /// 切换用户
-        /// </summary>
-        public void SwitchUser()
-        {
-
-        }
-
-
         #endregion
 
 
         #region 文件
 
-        public void GetFiles()
-        {
-
-        }
-
-        public void GetFolders()
-        {
-
-        }
-
-        public void UpLoad()
+        public async Task<bool> UpLoadFile(FileOperation fileOperation)
         {
+            bool result = false;
+            switch (fileOperation.User.cloudType)
+            {
+                case CloudType.GoogleDrive:
+                    {
+                        var googleUser = fileOperation.User as GoogleDriveUserItem;
+                        if (googleUser != null)
+                        {
+                            result = await GoogleDrive.UpLoadFile(fileOperation);
+                        }
+                    }
+                    break;
 
+                case CloudType.DropBox:
+                    {
+                        var dropBoxUser = fileOperation.User as DropbBoxUserItem;
+                        if (dropBoxUser != null)
+                        {
+                            result = await DropbBox.UpLoadFile(fileOperation);
+                        }
+                    }
+                    break;
+            }
+            return result;
         }
+     
         #endregion
 
     }

+ 9 - 23
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveType/DropbBoxManager.cs

@@ -48,25 +48,11 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             return result;
         }
 
-        /// <summary>
-        /// 切换用户
-        /// </summary>
-        public void SwitchUser()
-        {
-
-        }
-
-
         #endregion
 
 
         #region 文件
 
-        public void GetFiles()
-        {
-
-        }
-
         public async Task<List<DropbBoxFiles>> GetFolder(FileOperation fileOperation)
         {
             var user = fileOperation.User as DropbBoxUserItem;
@@ -85,9 +71,16 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             return null;
         }
 
-        public void UpLoad()
+        public async Task<bool> UpLoadFile(FileOperation fileOperation)
         {
-
+            bool result = false;
+            var user = fileOperation.User as DropbBoxUserItem;
+            if (user != null && fileOperation.DoFile as DropbBoxFiles != null)
+            {
+                var pathDisplay = (fileOperation.DoFile as DropbBoxFiles).PathDisplay;
+                result = await user.UpLoadFile(fileOperation.SourceFile, fileOperation.SourceFileName,pathDisplay);
+            }
+            return result;
         }
 
         public async Task<string> DownloadFile(FileOperation fileOperation, string savePath)
@@ -102,12 +95,5 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             return result;
         }
         #endregion
-
-
-
-
-
-
-
     }
 }

+ 15 - 38
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveType/DropbBoxUserItem.cs

@@ -49,6 +49,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
                     DropbBoxFiles dropbBoxFiles = new DropbBoxFiles();
                     dropbBoxFiles.Name = file.Name;
                     dropbBoxFiles.IsFolder = file.IsFolder;
+                    dropbBoxFiles.PathDisplay = file.PathDisplay;
                     if (file.IsFolder)
                     {
                         if(file.AsFolder != null)
@@ -139,47 +140,23 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             return memoryStream;
         }
 
-        private void Upload()
+        public async Task<bool> UpLoadFile(string sourceFilePath,string sourceFileName,string pathDisplay)
         {
-            //if (ItemList.SelectedItem != null)
-            //{
-            //    var item = ItemList.SelectedItem as Metadata;
-            //    if (item.IsFolder)
-            //        return;
-
-            //OpenFileDialog openFileDialog = new OpenFileDialog();
-            //if ((bool)openFileDialog.ShowDialog())
-            //{
-            //    string folder = currentFolder; /*= item.PathDisplay.Replace("/" + item.Name, "");*/
-            //    using (var stream = System.IO.File.OpenRead(openFileDialog.FileName))
-            //    {
-            //        await client.Files.UploadAsync(folder + "/" + openFileDialog.SafeFileName, WriteMode.Overwrite.Instance, body: stream);
-            //    }
-            //    MessageBox.Show("上传完成");
-            //    RefreshList(folder);
-            //}
-            //}
-        }
 
-        private void Loaded()
-        {
-            //var text = sender as TextBlock;
-            //if (text == null)
-            //    return;
-            //if (text.DataContext is Metadata)
-            //{
-            //    var data = (text.DataContext as Metadata);
-            //    if (data.IsFile)
-            //    {
-            //        text.Text = "File";
-            //    }
-            //    if (data.IsFolder)
-            //    {
-            //        text.Text = "Folder";
-            //    }
-            //}
-        }
+            if (string.IsNullOrEmpty(pathDisplay) == false)
+            {
+                using (var stream = System.IO.File.OpenRead(sourceFilePath))
+                {
+                    await client.Files.UploadAsync(pathDisplay + "/" + sourceFileName, WriteMode.Overwrite.Instance, body: stream);
+                }
+                return true;
+            }
+            else
+            {
+                return false;
+            }
 
+        }
 
     }
 

+ 16 - 1
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveType/GoogleDriveManager.cs

@@ -28,7 +28,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
         #region   请求身份验证
 
         /// <summary>
-        /// 获取登录过的用户
+        /// 预留需求:获取登录过的用户
         /// </summary>
         public async Task<bool> GetHistoryUsers()
         {
@@ -106,6 +106,21 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
 
         #region  文件
 
+        public async Task<bool> UpLoadFile(FileOperation fileOperation)
+        {
+            bool result = false;
+            var user = fileOperation.User as GoogleDriveUserItem;
+            if (user != null)
+            {
+                if (fileOperation.DoFile != null)
+                    result = await user.FileUpload(fileOperation.SourceFile, fileOperation.DoFile.Id);
+                else
+                    result = await user.FileUpload(fileOperation.SourceFile,"");
+
+            }
+            return result;
+        }
+
         public async Task<string> DownloadGoogleFile(FileOperation fileOperation, string savePath)
         {
             string result = "";

+ 8 - 16
PDF Office/ViewModels/HomePanel/CloudDrive/CloudDriveType/GoogleDriveUserItem.cs

@@ -89,14 +89,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
 
         #endregion
 
-
-        #region   文件夹
-
-
-
-        #endregion
-
-        #region  文件
+        #region  文件夹、文件
 
         /// <summary>
         /// 对单个用户,获取文件列表
@@ -176,16 +169,12 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             return filelist;
         }
 
-        private void SearchParentFiles()
-        {
-
-        }
 
         /// <summary>
         /// 上传文件
         /// </summary>
         /// <param name="filepath">本地文件路径</param>
-        public  async Task<bool> FileUpload(string filepath)
+        public  async Task<bool> FileUpload(string filepath,string desFolderId)
         {
             if (Service == null)
                 return false;
@@ -195,7 +184,12 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
             var str2 = filepath.Substring(str + 1, filepath.Length - str - 1);
 
             FileMetaData.Name = str2;
-            FileMetaData.MimeType = "";
+            FileMetaData.MimeType =GoogleDriveStatic.MimeType;
+
+            if (string.IsNullOrEmpty(desFolderId) == false)
+            {
+                FileMetaData.Parents = new List<string> { desFolderId };
+            }
 
             FilesResource.CreateMediaUpload request;
             try
@@ -206,7 +200,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
                     request.Fields = "id";
                     request.Upload();
                 }
-
                 return true;
             }
             catch
@@ -287,7 +280,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
         #endregion
     }
 
-
     public static class GoogleDriveStatic
     {
         public static string[] Scopes = { DriveService.Scope.Drive };

+ 89 - 85
PDF Office/ViewModels/HomePanel/CloudDrive/CloudFilesContentViewModel.cs

@@ -1,4 +1,5 @@
-using PDF_Office.Helper;
+using Microsoft.Win32;
+using PDF_Office.Helper;
 using PDF_Office.Model.CloudDrive;
 using PDF_Office.Properties;
 using PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType;
@@ -10,6 +11,7 @@ using System.Collections.ObjectModel;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
 
 namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 {
@@ -42,14 +44,27 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         #region  事件
         /// <summary>
+        /// 根据目录,更新文件列表
+        /// </summary>
+        public DelegateCommand<FileOperation> OpenFolderCommand { get; set; }
+
+        /// <summary>
+        /// 选中文件夹,更新指定文件夹里的文件列表
+        /// </summary>
+        public DelegateCommand<FolderBaseItem> SelectedFolderCommand { get; set; }
+        /// <summary>
         /// 下载并打开文件
         /// </summary>
         public DelegateCommand<FileOperation> OpenFileCommand { get; set; }
-        public DelegateCommand<FileOperation> OpenFolderCommand { get; set; }
+
+        /// <summary>
+        /// 上传文件
+        /// </summary>
+        public DelegateCommand<FileOperation> UpLoadFileCommand { get; set; }
+
         public DelegateCommand<CloudBoxItem> LoginCommand { get; set; }
         public DelegateCommand<UserBaseItem> RemoveUserCommand { get; set; }
 
-        public DelegateCommand<FolderBaseItem> SelectedFolderCommand { get; set; }
         #endregion
 
         public CloudFilesContentViewModel()
@@ -66,69 +81,27 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         private void InitCommand()
         {
-            OpenFileCommand = new DelegateCommand<FileOperation>(OpenFile);
             OpenFolderCommand = new DelegateCommand<FileOperation>(OpenFolder);
-            LoginCommand = new DelegateCommand<CloudBoxItem>(LoginUser);
-            RemoveUserCommand = new DelegateCommand<UserBaseItem>(RemoveUser);
-            SelectedFolderCommand = new DelegateCommand<FolderBaseItem>(SelectedFolder_Command);
-        }
-
-        /// <summary>
-        /// 在文件夹列表,选择文件夹
-        /// </summary>
-        private void SelectedFolder_Command(FolderBaseItem obj)
-        {
-           if(obj != null)
-            {
-                var objIndex = FolderItems.IndexOf(obj);
-                var count = FolderItems.Count;
-
-                if (objIndex == count - 1)
-                    return;
+            SelectedFolderCommand = new DelegateCommand<FolderBaseItem>(SelectedFolder);
 
-                if (obj.Level == -1)
-                {
-                    
-                    for(int i = 1; i < count; i++)
-                    {
-                        FolderItems.RemoveAt(i);
-                    }
-                }
-                else
-                {
-                    if(objIndex != -1)
-                    {
-                        for(int i = count - 1; i > objIndex; i--)
-                        {
-                            FolderItems.Remove(FolderItems[i]);
-                        }
-                    }
-                }
-
-                SelectedFolder(obj.Operation);
-            }
-        }
+            OpenFileCommand = new DelegateCommand<FileOperation>(OpenFile);
+            UpLoadFileCommand = new DelegateCommand<FileOperation>(UpLoadFile);
 
+            LoginCommand = new DelegateCommand<CloudBoxItem>(LoginUser);
+            RemoveUserCommand = new DelegateCommand<UserBaseItem>(RemoveUser);
 
-        public async void CheckDrive(CloudBoxItem cloudDriveItem)
-        {
-            await CloudManager.LoginUser(cloudDriveItem.CloudDiskType);
         }
 
         #region 云盘公用接口
 
         #endregion
-        public void OpenCloudDrive_Click()
-        {
-
-        }
 
         #region 用户帐号
 
         /// <summary>
         /// 登录
         /// </summary>
-        public async void LoginUser(CloudBoxItem cloudDriveItem)
+        private async void LoginUser(CloudBoxItem cloudDriveItem)
         {
             if (cloudDriveItem != null)
                 await CloudManager.LoginUser(cloudDriveItem.CloudDiskType);
@@ -138,7 +111,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
         /// <summary>
         /// 移除用户
         /// </summary>
-        public async void RemoveUser(UserBaseItem user)
+        private async void RemoveUser(UserBaseItem user)
         {
             if(user != null)
             {
@@ -151,65 +124,68 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
             }
         }
 
-        /// <summary>
-        /// 切换用户
-        /// </summary>
-        public void SwitchUser()
-        {
-
-        }
-
-
         #endregion
 
 
         #region 文件
-        
-        /// <summary>
-        /// 获取文件夹和文件的列表
-        /// </summary>
-        public void GetFiles()
-        {
 
+        private async void OpenFolder(FileOperation fileOperation)
+        {
+            FilesList = await CloudManager.OpenFolder(fileOperation);
+            AddFolder(fileOperation);
         }
 
         /// <summary>
-        /// 选择为当前目录
+        /// 在文件夹列表,选择文件夹
         /// </summary>
-        public void SelectedFolder()
+        private void SelectedFolder(FolderBaseItem obj)
         {
+            if (obj != null)
+            {
+                var objIndex = FolderItems.IndexOf(obj);
+                var count = FolderItems.Count;
 
-        }
+                if (objIndex == count - 1)
+                    return;
 
-        /// <summary>
-        /// 下载并打开文件
-        /// </summary>
-        public async void OpenFile(FileOperation fileOperation)
-        {
-            await CloudManager.OpenFile(fileOperation);
-        }
+                if (obj.Level == -1)
+                {
 
-        public async void OpenFolder(FileOperation fileOperation)
-        {
-            FilesList = await CloudManager.OpenFolder(fileOperation);
-            AddFolder(fileOperation);
+                    for (int i = 1; i < count; i++)
+                    {
+                        FolderItems.RemoveAt(i);
+                    }
+                }
+                else
+                {
+                    if (objIndex != -1)
+                    {
+                        for (int i = count - 1; i > objIndex; i--)
+                        {
+                            FolderItems.Remove(FolderItems[i]);
+                        }
+                    }
+                }
+
+                SelectedFolder(obj.Operation);
+            }
         }
 
         /// <summary>
         /// 在文件列表点击文件夹,而更新文件列表
         /// </summary>
         /// <param name="fileOperation"></param>
-        public async void SelectedFolder(FileOperation fileOperation)
+        private async void SelectedFolder(FileOperation fileOperation)
         {
             FilesList = await CloudManager.OpenFolder(fileOperation);
         }
 
         private void AddFolder(FileOperation folder)
         {
-          
+
             if (folder != null)
             {
-               if(folder.DoFile != null)
+                if (folder.DoFile != null)
                 {
                     FolderBaseItem folderItem = new FolderBaseItem();
                     folderItem.FolderName = folder.DoFile.Name;
@@ -229,12 +205,40 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
             }
         }
 
+        /// <summary>
+        /// 下载并打开文件
+        /// </summary>
+        private async void OpenFile(FileOperation fileOperation)
+        {
+            await CloudManager.OpenFile(fileOperation);
+        }
 
         /// <summary>
         /// 上传文件
         /// </summary>
-        public void UpLoadFile()
+        private async void UpLoadFile(FileOperation fileOperation)
         {
+            OpenFileDialog openFileDialog = new OpenFileDialog();
+            openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
+            openFileDialog.Multiselect = true;
+            if ((bool)openFileDialog.ShowDialog())
+            {
+                if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
+                {
+                    fileOperation.SourceFile = openFileDialog.FileName;
+                    fileOperation.SourceFileName = openFileDialog.SafeFileName;
+                    var result = await CloudManager.UpLoadFile(fileOperation);
+                    if (result)
+                    {
+                        FilesList = await CloudManager.OpenFolder(fileOperation);
+                        MessageBox.Show("文件上传完成");
+                    }
+                    else
+                    {
+                        MessageBox.Show("文件上传失败");
+                    }
+                }
+            }
 
         }
         #endregion

+ 2 - 1
PDF Office/Views/HomePanel/CloudDrive/CloudFilesContent.xaml

@@ -38,7 +38,7 @@
                     <Setter Property="VerticalContentAlignment" Value="Center"/>
                 </Style>
             </ContextMenu.ItemContainerStyle>
-            <MenuItem Name="OpenDocMenuItem" Header="移除帐户" IsEnabled="True" Click="RemoveUserMenuItem_Click">
+            <MenuItem Name="RemoveUserMenuItem" Header="移除帐户" IsEnabled="True" Click="RemoveUserMenuItem_Click">
                 <MenuItem.Icon>
                     <Path Fill="Black" Data="M9 0H3V2H0V3H1V14H11V3H12V2H9V0ZM2 13V3H3H4H8H9H10V13H2ZM8 2V1H4V2H8ZM4 12V4H3V12H4ZM6.5 4V12H5.5V4H6.5ZM9 12V4H8V12H9Z">
                         <Path.RenderTransform>
@@ -109,6 +109,7 @@
                         </i:EventTrigger>
                     </i:Interaction.Triggers>
                 </ComboBox>
+                <Button x:Name="UpLoadFileBtn" Content="上传文件" Click="UpLoadFileBtn_Click"/>
             </StackPanel>
            
         </Grid>

+ 10 - 0
PDF Office/Views/HomePanel/CloudDrive/CloudFilesContent.xaml.cs

@@ -181,5 +181,15 @@ namespace PDF_Office.Views.HomePanel.CloudDrive
             }
         }
 
+        private void UpLoadFileBtn_Click(object sender, RoutedEventArgs e)
+        {
+            var folder = ListFolder.Items[ListFolder.Items.Count - 1] as FolderBaseItem;
+            if (folder != null && folder.Operation != null)
+            {
+                FileOperation fileOperation = new FileOperation(CurrentUser, folder.Operation.DoFile);
+                ViewModel?.UpLoadFileCommand?.Execute(fileOperation);
+            }
+
+        }
     }
 }