예제 #1
0
 public string ToAlignedJson(BookmarkHierarchyElement hierarchy)
 {
     if (hierarchy is FilesGroup)
     {
         return(ToJson(hierarchy, true)); // no aligning for one bookmark
     }
     if (hierarchy is Folder)
     {
         var jsonString = ToJson(hierarchy, true);
         return(JArray.Parse(jsonString).ToString(Formatting.Indented));
     }
     throw new ArgumentOutOfRangeException(nameof(hierarchy));
 }
예제 #2
0
        private string GetFullPath(BookmarkHierarchyElement hierarchyElement)
        {
            var partialPath = hierarchyElement.LocalPath;

            if (partialPath.Length > 0)
            {
                var firstChar = partialPath[0];
                if (firstChar == '\\' || firstChar == '/')
                {
                    partialPath = partialPath.Substring(1);
                }
            }
            return(Path.Combine(_hierarchyRoot, partialPath).Replace('\\', '/'));
        }
예제 #3
0
        public override async Task <bool> Move(BookmarkHierarchyElement element, string newPath)
        {
            if (element is FakeFilesGroup)
            {
                throw new Exception("can't move thing that doesn't exist!");
            }
            if (element is FilesGroup filesGroup)
            {
                if (await Find(newPath).ConfigureAwait(false) != null)
                {
                    return(false);
                }
                var fake           = MakeFake(newPath);
                var newFilesGroup  = new ProxyFilesGroup(fake, filesGroup);
                var saveSuccessful = await Save(newFilesGroup, FileWriteMode.CreateNew).ConfigureAwait(false);

                if (!saveSuccessful)
                {
                    return(false);
                }
                return(await DeleteBookmark(filesGroup).ConfigureAwait(false));
            }
            if (element is Folder folder)
            {
                if (await FindFolder(newPath).ConfigureAwait(false) != null)
                {
                    return(false); // it exists
                }
                var oldFolderFullPath = GetFullPath(folder);
                var newFolderFullPath = GetFullPath(newPath);
                var directoryInfo     = new DirectoryInfo(oldFolderFullPath);
                try
                {
                    // adding and deleting file for invoking event (needed for FolderChangeWatcher)
                    var touchFileInfo = new FileInfo($"{oldFolderFullPath}\\{Guid.NewGuid()}.touch");
                    touchFileInfo.Create().Dispose();
                    touchFileInfo.Delete();

                    directoryInfo.Attributes = FileAttributes.Normal;
                    directoryInfo.MoveTo(newFolderFullPath);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
            throw new ArgumentException("derived type isn't recognized!");
        }
예제 #4
0
 public string ToJson(BookmarkHierarchyElement hierarchy, bool serializeAsRoot)
 {
     if (hierarchy is FilesGroup bookmarkHierarchyBookmark)
     {
         return($"\"{bookmarkHierarchyBookmark.Name}{SeparatorChar}{bookmarkHierarchyBookmark.LastTime:o}\"");
     }
     if (hierarchy is Folder bookmarkHierarchyFolder)
     {
         var listOfSerializedChildren = string.Join(",", bookmarkHierarchyFolder.Children.Select(folder => ToJson(folder, false)));
         return(serializeAsRoot
             ? $"[{listOfSerializedChildren}]"
             : $"{{\"{bookmarkHierarchyFolder.Name}\":[{listOfSerializedChildren}]}}");
     }
     throw new ArgumentOutOfRangeException(nameof(hierarchy));
 }
예제 #5
0
        public override async Task <bool> Move(BookmarkHierarchyElement element, string newPath)
        {
            var originalFullPath    = GetFullPath(element);
            var originalPathContent = new StringContent(originalFullPath);

            var newFullPath    = GetFullPath(newPath);
            var newPathContent = new StringContent(newFullPath);

            var isFolder        = element is Folder;
            var isFolderContent = new StringContent(isFolder.ToString());

            var multipartFormDataContent = new MultipartFormDataContent
            {
                { originalPathContent, "originalPath" },
                { newPathContent, "newPath" },
                { isFolderContent, "isFolder" },
            };

            using var httpRequestMessage = await CreateHttpRequestMessageWithAuthHeader(HttpMethod.Post, Endpoints.MoveEndpoint, multipartFormDataContent).ConfigureAwait(false);

            using var responseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

            return(responseMessage.IsSuccessStatusCode);
        }
예제 #6
0
 private string GetFullPath(BookmarkHierarchyElement hierarchyElement)
 {
     return(GetFullPath(hierarchyElement.LocalPath));
 }
예제 #7
0
 public abstract Task <bool> Move(BookmarkHierarchyElement element, string newPath);