Esempio n. 1
0
        protected override void PerformAddOrUpdate(TypedEntity persistedEntity)
        {
            Mandate.That <ArgumentException>(typeof(File).IsAssignableFrom(persistedEntity.GetType()));
            var file = (File)persistedEntity;

            Mandate.That <ArgumentNullException>(!file.Name.IsNullOrWhiteSpace() || !file.RootedPath.IsNullOrWhiteSpace());

            // Make sure that the incoming location has the root storage area prefixed if necessary
            file.RootedPath = EnsureLocationRooted(string.IsNullOrEmpty(file.RootedPath) ? file.Name : file.RootedPath);

            //ensure the file name is set which should always be based on the location
            file.Name = Path.GetFileName(file.RootedPath);

            //get a reference to the previous filename from the id before we update it
            var oldFileName = !file.Id.IsNullValueOrEmpty() ? file.Id.Value.ToString() : null;

            // Set the id if it is a new file, or reset it incase the file name has changed
            file.Id = GenerateId(file.RootedPath);

            var writeSuccess = false;

            try
            {
                // Ensure that the folder exists, if this item is a folder)
                if (file.IsContainer)
                {
                    var dir = new DirectoryInfo(file.RootedPath);
                    if (!dir.Exists)
                    {
                        dir.Create();
                        writeSuccess = true;
                    }
                }
                else
                {
                    var containerPath = Path.GetDirectoryName(file.RootedPath);
                    if (containerPath != null)
                    {
                        var dir = new DirectoryInfo(containerPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }
                    }
                }

                // Write the file, provided it's not a directory
                if (!file.IsContainer)
                {
                    if (!string.IsNullOrWhiteSpace(oldFileName))
                    {
                        var oldFilePath = EnsureLocationRooted(oldFileName);
                        var oldFileInfo = new FileInfo(oldFilePath);
                        if (oldFileInfo.Exists)
                        {
                            oldFileInfo.Delete();
                        }
                    }

                    var physicalForWriting = new FileInfo(file.RootedPath);
                    if (physicalForWriting.Exists)
                    {
                        physicalForWriting.Delete();
                    }

                    using (var writer = physicalForWriting.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
                    {
                        var content = file.ContentBytes.StripUTF8BOMs();

                        writer.Write(content, 0, content.Length);

                        writeSuccess = true;
                    }
                }
            }
            finally
            {
                if (writeSuccess)
                {
                    // Make sure to update relation caches
                    EnsureNotifyRelationCache(file.Id);
                }
            }
        }