Example #1
0
        /// <summary>
        /// Deserializes all .item files below the item's children folder and all deeper directories.
        /// Also traverses shortened paths.
        /// </summary>
        /// <param name="itemFile"></param>
        /// <param name="syncItem"></param>
        /// <param name="serializationFolder"></param>
        /// <param name="maxDepth"></param>
        /// <returns></returns>
#pragma warning disable 618
        internal static List <SyncItem> DeserializeAll(this FileInfo itemFile, SyncItem syncItem, DirectoryInfo serializationFolder, int maxDepth)
#pragma warning restore 618
        {
            if (maxDepth <= 0)
            {
#pragma warning disable 618
                return(new List <SyncItem>());

#pragma warning restore 618
            }

            Assert.ArgumentNotNull(itemFile, "itemFile");

#pragma warning disable 618
            var result = new List <SyncItem>();
#pragma warning restore 618

            // Find descendants in direct subfolder
            if (itemFile.Directory != null)
            {
                var childItemsFolder = new DirectoryInfo(itemFile.Directory.FullName + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(itemFile.Name));
                if (childItemsFolder.Exists)
                {
                    foreach (var childItemFile in childItemsFolder.GetFiles("*.item", SearchOption.AllDirectories))
                    {
                        var childSyncItem = childItemFile.Deserialize();
                        result.AddRange(childItemFile.DeserializeAll(childSyncItem, serializationFolder, maxDepth - 1));
                        result.Add(childSyncItem);
                    }
                }
            }

            // Find descendants in shortened paths
            var linkFiles = ShortenedPathsDictionary.GetLocationsFromLinkFiles(serializationFolder);
            if (!linkFiles.ContainsKey(syncItem.ItemPath))
            {
                return(result);
            }

            var truePath = new DirectoryInfo(linkFiles[syncItem.ItemPath]);
            if (!truePath.Exists)
            {
                return(result);
            }

            foreach (var childItemFile in truePath.GetFiles("*.item", SearchOption.AllDirectories))
            {
                var childSyncItem = childItemFile.Deserialize();
                result.AddRange(childItemFile.DeserializeAll(childSyncItem, serializationFolder, maxDepth - 1));
                result.Add(childSyncItem);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Resolves a sitecore path to the filesystem where it is serialized.
        /// For example, path=/sitecore/content/item1 and serializationFolderName=master
        /// Will be resolved to c:\site\data\serialization\master\sitecore\content\item1.item
        ///
        /// You need to define the serialization folders in the app.config. For example:
        /// <szfolders>
        ///   <folder name="core" value="c:\site\data\serialization\core\" />
        ///   <folder name="master" value="c:\site\data\serialization\master\" />
        ///   <folder name="web" value="c:\site\data\serialization\web\" />
        /// </szfolders>
        /// </summary>
        /// <param name="path">A valid sitecore item path</param>
        /// <param name="serializationFolder">Folder in which serialized items are available</param>
        /// <returns></returns>
        internal static FileInfo ResolveSerializationPath(string path, DirectoryInfo serializationFolder)
        {
            var truePath = ShortenedPathsDictionary.FindTruePath(serializationFolder, path);

            var itemLocation =
                new FileInfo(
                    string.Format(
                        "{0}.item",
                        Path.Combine(
                            serializationFolder.FullName.Trim(new[] { Path.DirectorySeparatorChar }),
                            truePath.Replace('/', Path.DirectorySeparatorChar).Trim(new[] { Path.DirectorySeparatorChar }))));

            Assert.IsTrue(
                itemLocation.Exists,
                string.Format("Serialized item '{0}' could not be found in the path '{1}'; please check the path and if the item is serialized correctly", truePath, itemLocation.FullName));

            return(itemLocation);
        }
        public static string FindFilePath(this ID id, string serializationFolderName)
        {
            lock (PathSets)
            {
                // find or create the set of paths for the serialization folder
                SerializedIdToPathSet pathSet;
                if (PathSets.ContainsKey(serializationFolderName))
                {
                    pathSet = PathSets[serializationFolderName];
                }
                else
                {
                    pathSet = new SerializedIdToPathSet();
                    var serializationFolder = Deserializer.GetSerializationFolder(serializationFolderName);

                    // Add filepaths for shortened paths
                    foreach (
                        var shortenedItemsFolder in ShortenedPathsDictionary.GetLocationsFromLinkFiles(serializationFolder).Values)
                    {
                        pathSet.FilePaths.Push(shortenedItemsFolder);
                    }

                    // Add filepath for root of regular content
                    pathSet.FilePaths.Push(serializationFolder.FullName);

                    PathSets.Add(serializationFolderName, pathSet);
                }

                // get the individual item if already found
                if (pathSet.Paths.ContainsKey(id))
                {
                    return(pathSet.Paths[id]);
                }

                while (pathSet.FilePaths.Any())
                {
                    var filePath = pathSet.FilePaths.Pop();
                    foreach (var subdirectory in Directory.GetDirectories(filePath))
                    {
                        pathSet.FilePaths.Push(subdirectory);
                    }

                    string foundFile = null;
                    foreach (var file in Directory.GetFiles(filePath, "*.item"))
                    {
                        using (var sr = new StreamReader(file))
                        {
                            sr.ReadLine();
                            sr.ReadLine();
                            var itemIdStr = sr.ReadLine().Substring(4);
                            if (!ID.IsID(itemIdStr))
                            {
                                continue;
                            }

                            var itemId = ID.Parse(itemIdStr);
                            if (pathSet.Paths.ContainsKey(itemId))
                            {
                                throw new DuplicateIdException(itemId, file, pathSet.Paths[itemId]);
                            }

                            pathSet.Paths.Add(itemId, file);
                            if (itemId == id)
                            {
                                foundFile = file;
                            }
                        }
                    }

                    if (foundFile != null)
                    {
                        return(foundFile);
                    }
                }

                return(null);
            }
        }