Pārlūkot izejas kodu

云文档 - 调整管理云文档逻辑

chenrongqian 2 gadi atpakaļ
vecāks
revīzija
cb86ef123a

+ 4 - 0
PDF Office/Model/CloudDrive/CloudFiles.cs

@@ -13,6 +13,8 @@ namespace PDF_Office.Model.CloudDrive
     {
     }
 
+    #region GooglrDrive
+
     public class GoogleDriveUser
     {
         public string Name { get; set; }
@@ -34,4 +36,6 @@ namespace PDF_Office.Model.CloudDrive
         public DateTime? CreatedTime { get; set; }
     }
 
+    #endregion
+
 }

+ 94 - 10
PDF Office/ViewModels/HomePanel/CloudDrive/DropbBoxUserItem.cs

@@ -1,6 +1,8 @@
 using Dropbox.Api;
+using Dropbox.Api.Files;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Net;
 using System.Text;
@@ -28,7 +30,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
         private string currentFolder = "";
 
         HttpListener http = new HttpListener();
-        private async void btnConnect_Click()
+        private async void Connect()
         {
             DropboxCertHelper.InitializeCertPinning();
             var state = Guid.NewGuid().ToString("N");
@@ -37,16 +39,9 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
             //var http = new HttpListener();
             http.Prefixes.Clear();
             http.Prefixes.Add(LoopbackHost);
-
-
             http.Start();
-
-            //    IAsyncResult result = http.BeginGetContext(new AsyncCallback(ListenerCallback), http);
-
-
             System.Diagnostics.Process.Start(authorizeUri.ToString());
 
-
             await HandleOAuth2Redirect(http);
 
             // Handle redirect from JS and process OAuth response.
@@ -63,8 +58,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         }
 
-
-
         public static void ListenerCallback(IAsyncResult result)
         {
             HttpListener listener = (HttpListener)result.AsyncState;
@@ -145,5 +138,96 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
 
 
+        private void Download()
+        {
+            //if (ItemList.SelectedItem != null)
+            //{
+            //    var item = ItemList.SelectedItem as Metadata;
+            //    if (item.IsFolder)
+            //        return;
+
+            //    SaveFileDialog saveFileDialog = new SaveFileDialog();
+            //    saveFileDialog.FileName = item.Name;
+            //    if ((bool)saveFileDialog.ShowDialog())
+            //    {
+            //        string folder = item.PathDisplay.Replace("/" + item.Name, "");
+            //        using (var response = await client.Files.DownloadAsync(item.PathDisplay))
+            //        {
+            //            var result = await response.GetContentAsStreamAsync();
+            //            var stream = StreamToMemoryStream(result);
+            //            using (var fileStream = System.IO.File.Create(saveFileDialog.FileName))
+            //            {
+            //                stream.Seek(0, System.IO.SeekOrigin.Begin);
+            //                stream.CopyTo(fileStream);
+            //            }
+            //        }
+            //        MessageBox.Show("下载完成");
+            //        System.Diagnostics.Process.Start(@"explorer.exe", "/select,\"" + saveFileDialog.FileName + "\"");
+            //        RefreshList(folder);
+            //    }
+            //}
+        }
+
+        public MemoryStream StreamToMemoryStream(Stream stream)
+        {
+            MemoryStream memoryStream = new MemoryStream();
+
+            //将基础流写入内存流
+            const int bufferLength = 1024;
+            byte[] buffer = new byte[bufferLength];
+            int actual = stream.Read(buffer, 0, bufferLength);
+            while (actual > 0)
+            {
+                // 读、写过程中,流的位置会自动走。
+                memoryStream.Write(buffer, 0, actual);
+                actual = stream.Read(buffer, 0, bufferLength);
+            }
+            memoryStream.Position = 0;
+
+            return memoryStream;
+        }
+
+        private void Upload()
+        {
+            //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";
+            //    }
+            //}
+        }
+
+
     }
 }

+ 3 - 116
PDF Office/ViewModels/HomePanel/CloudDrive/GoogleDriveManager.cs

@@ -15,13 +15,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
     //多用户UX交互
     public class GoogleDriveManager
     {
-        public static string[] Scopes = { DriveService.Scope.Drive };
-        //Google Drive应用名称
-        private static readonly string GoogleDriveAppName = "PDF Office";
-        //请求应用进行身份验证的信息
-        public static string CredentialsPath { get; private set; }
-        //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档
-        public static string FilesPathTemp { get; private set; }
 
         //已登录的用户们
         public List<GoogleDriveUserItem> GoogleDriveUsers = new List<GoogleDriveUserItem>();
@@ -29,40 +22,10 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         public GoogleDriveManager()
         {
-            GetFilesPathTemp();
-            GetCredentialsPath();
+             GoogleDriveStatic.GetFilesPathTemp();
+             GoogleDriveStatic.GetCredentialsPath();
         }
 
-
-        #region  云文档的用户帐户缓存路径和身份验证文件
-
-        /// <summary>
-        /// 获取或创建缓存登录帐户信息
-        /// </summary>
-        public void GetFilesPathTemp()
-        {
-            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
-            String FolderPath = str_1 + "GoogleDriveUsers";
-
-            if (Directory.Exists(FolderPath) == false)
-                Directory.CreateDirectory(FolderPath);
-
-            FilesPathTemp = FolderPath;
-        }
-
-        /// <summary>
-        /// 获取本地身份验证文件
-        /// </summary>
-        public void GetCredentialsPath()
-        {
-            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
-            String filePath = str_1 + @"\credentials.json";
-            CredentialsPath = filePath;
-        }
-
-        #endregion
-
-
         #region   请求身份验证
 
         /// <summary>
@@ -70,7 +33,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
         /// </summary>
         public async Task<bool> GetHistoryUsers()
         {
-            var tuples = await GetHistoryService();
+            var tuples = await GoogleDriveStatic.GetHistoryService();
             GoogleDriveUsers.Clear();
             foreach (var tuple in tuples)
             {
@@ -98,82 +61,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
         }
 
 
-
-        /// <summary>
-        /// 获取登录过的账号
-        /// </summary>
-        /// <returns>历史账号</returns>
-        private async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
-        {
-            DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveManager.FilesPathTemp);
-            List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
-
-            foreach (var directorieItem in TheFolder.GetDirectories())
-            {
-                var driveServiceItem = await GetServiceAsync(directorieItem.Name);
-
-                if (driveServiceItem != null)
-                {
-                    DriveServices.Add(driveServiceItem);
-                }
-            }
-            return DriveServices;
-        }
-
-        /// <summary>
-        ///  异步获取Google服务的包信息,避免UI线程卡死
-        /// </summary>
-        /// <param name="userInfoFile"></param>
-        [Obsolete]
-        public async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
-        {
-            Tuple<DriveService, UserCredential> tuple = null;
-            await Task.Run(() =>
-            {
-                tuple = GetService(userInfoFile);
-            });
-            return tuple;
-        }
-
-        /// <summary>
-        /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
-        /// </summary>
-        /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
-        /// <returns></returns>
-        [Obsolete]
-        private Tuple<DriveService, UserCredential> GetService(string FilePath = "")
-        {
-            Tuple<DriveService, UserCredential> tuple = null;
-            UserCredential credential;
-
-            if (FilePath == "")
-            {
-                var time = DateTime.Now.ToString("yyyyMMddHHmmss");
-                FilePath = System.IO.Path.Combine(GoogleDriveManager.FilesPathTemp, time + ".json");
-            }
-
-
-            using (var stream = new FileStream(GoogleDriveManager.CredentialsPath, FileMode.Open, FileAccess.Read))
-            {
-                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
-                    GoogleClientSecrets.Load(stream).Secrets,
-                    Scopes,
-                    "user",
-                    CancellationToken.None,
-                    new FileDataStore(FilePath, true)).Result;
-            }
-
-            //create Drive API service.
-            DriveService service = new DriveService(new BaseClientService.Initializer()
-            {
-                HttpClientInitializer = credential,
-                ApplicationName = GoogleDriveAppName
-            });
-
-            return tuple = new Tuple<DriveService, UserCredential>(service, credential);
-        }
-
-
         #endregion
     }
 }

+ 123 - 2
PDF Office/ViewModels/HomePanel/CloudDrive/GoogleDriveUserItem.cs

@@ -15,7 +15,9 @@ using System.Threading.Tasks;
 namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 {
 
-    //对单用户账号处理核心功能
+    /// <summary>
+    /// 对单账号处理的核心功能类
+    /// </summary>
     public class GoogleDriveUserItem
     {
         public GoogleDriveUser User { get;  set; }
@@ -28,7 +30,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
             
         }
       
-
         #region   对用户账号处理
 
         public async Task<bool> RemoveUser()
@@ -225,4 +226,124 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
 
         #endregion
     }
+
+
+    public static class GoogleDriveStatic
+    {
+        public static string[] Scopes = { DriveService.Scope.Drive };
+        //Google Drive应用名称
+        private static readonly string GoogleDriveAppName = "PDF Office";
+        //请求应用进行身份验证的信息
+        public static string CredentialsPath { get; private set; }
+
+        //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档
+        public static string FilesPathTemp { get; private set; }
+        /// <summary>
+        ///  异步获取Google服务的包信息,避免UI线程卡死
+        /// </summary>
+        /// <param name="userInfoFile"></param>
+        [Obsolete]
+        public static async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
+        {
+            Tuple<DriveService, UserCredential> tuple = null;
+            await Task.Run(() =>
+            {
+                tuple = GetService(userInfoFile);
+            });
+            return tuple;
+        }
+
+        /// <summary>
+        /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
+        /// </summary>
+        /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
+        /// <returns></returns>
+        [Obsolete]
+        public static Tuple<DriveService, UserCredential> GetService(string FilePath = "")
+        {
+            Tuple<DriveService, UserCredential> tuple = null;
+            UserCredential credential;
+
+            if (FilePath == "")
+            {
+                var time = DateTime.Now.ToString("yyyyMMddHHmmss");
+                FilePath = System.IO.Path.Combine(FilesPathTemp, time + ".json");
+            }
+
+
+            using (var stream = new FileStream(CredentialsPath, FileMode.Open, FileAccess.Read))
+            {
+                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
+                    GoogleClientSecrets.Load(stream).Secrets,
+                    Scopes,
+                    "user",
+                    CancellationToken.None,
+                    new FileDataStore(FilePath, true)).Result;
+            }
+
+            //create Drive API service.
+            DriveService service = new DriveService(new BaseClientService.Initializer()
+            {
+                HttpClientInitializer = credential,
+                ApplicationName = GoogleDriveAppName
+            });
+
+            return tuple = new Tuple<DriveService, UserCredential>(service, credential);
+        }
+
+
+        /// <summary>
+        /// 获取登录过的账号
+        /// </summary>
+        /// <returns>历史账号</returns>
+        public static async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
+        {
+            DirectoryInfo TheFolder = new DirectoryInfo(FilesPathTemp);
+            List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
+
+            foreach (var directorieItem in TheFolder.GetDirectories())
+            {
+                var driveServiceItem = await GetServiceAsync(directorieItem.Name);
+
+                if (driveServiceItem != null)
+                {
+                    DriveServices.Add(driveServiceItem);
+                }
+            }
+            return DriveServices;
+        }
+
+
+
+
+        #region  云文档的用户帐户缓存路径和身份验证文件
+
+        /// <summary>
+        /// 获取或创建缓存登录帐户信息
+        /// </summary>
+        public static string GetFilesPathTemp()
+        {
+            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
+            String FolderPath = str_1 + "GoogleDriveUsers";
+
+            if (Directory.Exists(FolderPath) == false)
+                Directory.CreateDirectory(FolderPath);
+
+            FilesPathTemp = FolderPath;
+            return FolderPath;
+        }
+
+        /// <summary>
+        /// 获取本地身份验证文件
+        /// </summary>
+        public static string GetCredentialsPath()
+        {
+            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
+            String filePath = str_1 + @"\credentials.json";
+            CredentialsPath = filePath;
+            return filePath;
+        }
+
+        #endregion
+    }
 }

+ 2 - 2
PDF Office/ViewModels/HomePanel/HomeCloudContentViewModel.cs

@@ -12,12 +12,12 @@ namespace PDF_Office.ViewModels.HomePanel
 {
     public class HomeCloudContentViewModel:BindableBase
     {
-        public GoogleDriveManager googleDriveViewModel;
+        public GoogleDriveManager googleDriveManager;
         
         public DelegateCommand<CloudDriveItem> OpenCloudDriveCommand { get; set; }
         public HomeCloudContentViewModel()
         {
-            googleDriveViewModel = new GoogleDriveManager();
+            googleDriveManager = new GoogleDriveManager();
             OpenCloudDriveCommand = new DelegateCommand<CloudDriveItem>(OpenCloudDrive_Click);
         }
 

+ 2 - 2
PDF Office/Views/HomePanel/HomeCloudContent.xaml.cs

@@ -30,8 +30,8 @@ namespace PDF_Office.Views.HomePanel
 
         private void HomeCloudContent_Loaded(object sender, RoutedEventArgs e)
         {
-           var cloudDrives = cloudFilesContent.DataContext as CloudFilesContentViewModel;
-            if(cloudDrives != null)
+            var cloudDrives = cloudFilesContent.DataContext as CloudFilesContentViewModel;
+            if (cloudDrives != null)
             {
                 cloudDrives.CheckDriveUsersCommand.Execute();
             }