private static FileAttributes ToAttributes(StateSyncEntry item)
        {
            var atts = FileAttributes.Normal;

            if (item.IsHidden)
            {
                atts |= FileAttributes.Hidden;
            }

            if (item.IsDirectory)
            {
                atts |= FileAttributes.Directory;
            }
            return(atts);
        }
        public Task DeleteEntryAsync(SyncContext context, StateSyncEntry entry, SyncDeleteEntryOptions options = null)
        {
            var item = new WebItem {
                Id = ToId(entry.Id)
            };

            if (item.Id == Guid.Empty)
            {
                return(Task.CompletedTask);
            }

            return(item.DeleteAsync(new DeleteOptions {
                Recursive = options?.Recursive == true
            }));
        }
        private GDriveData.File GetOrCreateFile(StateSyncEntry entry, SyncGetEntryOptions options)
        {
            GDriveData.File file;
            if (entry.Id == null)
            {
                if (!options.IsTemporary)
                {
                    return(null);
                }

                // entry is a temporary entry
                file = Account.GetFilesByName(Account.TempFolderId, entry.Name).FirstOrDefault();
                if (file != null)
                {
                    return(file);
                }

                Account.Log(TraceLevel.Warning, "Cannot find temp entry '" + entry.Name + "' with parent temp folder id '" + Account.TempFolderId + "'.");
                if (!options.CanCreate)
                {
                    return(null);
                }

                return(Account.CreateFile(entry.Name, Account.TempFolderId));
            }

            file = Account.GetFile(entry.Id);
            if (file != null)
            {
                return(file);
            }

            Account.Log(TraceLevel.Warning, "Cannot find entry with id '" + entry.Id + "' name '" + entry.Name + "'.");
            if (!options.CanCreate)
            {
                return(null);
            }

            // we can't create google docs
            if (IsGoogleDoc(entry))
            {
                return(null);
            }

            return(CreateEntry(entry));
        }
        public IEnumerable <StateSyncEntry> EnumerateEntries(SyncContext context, StateSyncEntry parentEntry, SyncEnumerateEntriesOptions options = null)
        {
            var item = new WebItem {
                Id = ToId(parentEntry.Id)
            };

            foreach (var child in WebApi.EnumerateChildren(item))
            {
                // never send back temp (being uploaded) files
                if (EndPointSynchronizer.MultiPointSynchronizer.ContentMover.IsTemporaryFile(child.Name))
                {
                    continue;
                }

                var entry = ToEntry(child);
                Logger?.Log(TraceLevel.Info, "entry '" + entry.Name + "'");
                yield return(entry);
            }
        }
        public IEnumerable <StateSyncEntry> EnumerateEntries(SyncContext context, StateSyncEntry parentEntry, SyncEnumerateEntriesOptions options = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (parentEntry == null)
            {
                throw new ArgumentNullException(nameof(parentEntry));
            }

            options = options ?? new SyncEnumerateEntriesOptions();
            foreach (var file in Account.GetFolderFiles(parentEntry.Id, options.Level == EnumerationLevel.AllChildren))
            {
                var entry = ToEntry(file);
                Account.Log(TraceLevel.Verbose, "entry " + entry.Id + " '" + entry.Name + "'");
                yield return(entry);
            }
        }
        public Task GetEntryContentAsync(SyncContext context, StateSyncEntry entry, Stream output, SyncGetEntryContentOptions options = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            options = options ?? new SyncGetEntryContentOptions();
            var webViewLink = entry.GetNullifiedExtendedDataValue("WebViewLink");

            Account.Log(TraceLevel.Verbose, "offset:" + options.Offset + " count:" + options.Count + " webViewLink:" + webViewLink);
            if (webViewLink != null)
            {
                var content = GetUrlFileContent(webViewLink);
                return(output.WriteAsync(content, (int)options.Offset, (int)Math.Min(options.Count, content.Length)));
            }

            var sink = context.ProgressSink;

            if (sink == null)
            {
                return(Account.DownloadFileAsync(entry.Id, options.Offset, options.Count, output, options.CancellationToken));
            }

            return(Account.DownloadFileAsync(entry.Id, options.Offset, options.Count, output, options.CancellationToken, (p) =>
            {
                sink.Progress(context, entry.Size, p.BytesDownloaded);
            }));
        }
        private void CopyToEntry(GDriveData.File file, StateSyncEntry entry)
        {
            if (file.MimeType == Account.FolderMimeType)
            {
                entry.Attributes |= SyncEntryAttributes.Directory;
            }

            // google docs are special: they have a zero size since they don't participate in user's quota
            // so we just create a .url file to redirect to the web
            if (Account.IsGoogleDoc(file))
            {
                entry.Name = file.Name + UrlExt;

                // .url format is
                // [InternetShortcut]
                // URL=https://developers.google.com/drive/api/v3/mime-types
                entry.Size = GetUrlFileContent(file.WebViewLink).Length;
                entry.SetExtendedDataValue("WebViewLink", file.WebViewLink);
            }
            else
            {
                entry.Name = file.Name;
                entry.Size = file.Size.GetValueOrDefault();
            }

            entry.ContentVersion = file.Version.GetValueOrDefault().ToString();
            if (file.CreatedTime.HasValue)
            {
                entry.CreationTime = file.CreatedTime.Value;
            }

            if (file.ModifiedTime.HasValue)
            {
                entry.LastWriteTime = file.ModifiedTime.Value;
            }

            entry.Id       = file.Id;
            entry.ParentId = file.Parents?.FirstOrDefault();
        }
        public void GetOrCreateEntry(SyncContext context, StateSyncEntry entry, SyncGetEntryOptions options = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            options = options ?? new SyncGetEntryOptions();
            var file = GetOrCreateFile(entry, options);

            if (file == null && !options.CanCreate)
            {
                return;
            }

            CopyToEntry(file, entry);
        }
        public async Task SetEntryContentAsync(SyncContext context, StateSyncEntry entry, Stream input, SyncSetEntryContentOptions options = null)
        {
            var item = new WebItem {
                Id = ToId(entry.Id)
            };

            if (item.Id == Guid.Empty)
            {
                return;
            }

            item.Attributes       = ToAttributes(entry);
            item.CreationTimeUtc  = entry.CreationTime.ToUniversalTime().UtcDateTime;
            item.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().UtcDateTime;
            item.Length           = entry.Size;
            item.Name             = entry.Name;
            item.ParentId         = ToId(entry.ParentId);
            var newItem = await item.UploadAsync(input).ConfigureAwait(false);

            if (newItem != null)
            {
                entry.Id = ToId(newItem.Id);
            }
        }
Beispiel #10
0
 private bool IsGoogleDoc(StateSyncEntry entry) => entry != null && entry.Name.EndsWith(UrlExt, StringComparison.OrdinalIgnoreCase);
 private static Task <WebItem> CreateEntryAsync(StateSyncEntry entry) => WebApi.CreateAsync(ToId(entry.ParentId), entry.Name, ToAttributes(entry));