示例#1
0
        public static async Task SaveDataAsync()
        {
            var localData = await GetDataAsync();

            var id = await GetFileIdAsync();

            var service = await DriveServiceSingleton.GetServiceAsync();

            var dataStream = new MemoryStream();

            new XmlSerializer(typeof(Data)).Serialize(dataStream, localData);
            dataStream.Position = 0;
            var file = new Google.Apis.Drive.v3.Data.File()
            {
                Name = DataFileName
            };
            IUploadProgress res;

            if (id == null)
            {
                file.Parents = new[] { DriveSpace };
                res          = await service.Files.Create(file, dataStream, DataMime).UploadAsync();
            }
            else
            {
                res = await service.Files.Update(file, id, dataStream, DataMime).UploadAsync();
            }
            if (res.Status == UploadStatus.Failed)
            {
                throw res.Exception;
            }
        }
示例#2
0
        private static async Task <string> GetFileIdAsync()
        {
            var listReq = (await DriveServiceSingleton.GetServiceAsync()).Files.List();

            listReq.Q      = $"name='{DataFileName}'";
            listReq.Spaces = DriveSpace;
            var list = await listReq.ExecuteAsync();

            return(list.Files.Count > 0 ? list.Files[0].Id : null);
        }
示例#3
0
        private static async Task <Data> DownloadDataAsync(string id)
        {
            await using var dataStream = new MemoryStream();
            var res = await(await DriveServiceSingleton.GetServiceAsync())
                      .Files.Get(id).DownloadAsync(dataStream);

            if (res.Status == DownloadStatus.Failed)
            {
                throw res.Exception;
            }
            dataStream.Position = 0;
            return((Data) new XmlSerializer(typeof(Data)).Deserialize(dataStream));
        }