Beispiel #1
0
        /// <summary>
        /// Write configuration to its file.
        /// </summary>
        /// <typeparam name="TConfig">Configuration type.</typeparam>
        /// <param name="config">Configuration instance.</param>
        public static void WriteConfig <TConfig>(TConfig config)
        {
            String fileName = GetConfigFileName <TConfig>();
            String filePath = SimplerPath.Combine(BaseDir, fileName);
            String json     = JsonConvert.SerializeObject(config);

            Io.File.WriteAllText(filePath, json);
        }
Beispiel #2
0
        /// <summary>
        /// Read configuration from file for a given config type.
        /// </summary>
        /// <param name="saveDefaultConfig">Write a new config file with default values if
        /// it doesn't exist?</param>
        /// <typeparam name="TConfig">Type of configuration.</typeparam>
        /// <returns>Configuration object.</returns>
        public static TConfig ReadConfig <TConfig>(Boolean saveDefaultConfig = false)
        {
            String fileName = GetConfigFileName <TConfig>();
            String filePath = SimplerPath.Combine(BaseDir, fileName);

            TConfig config;

            if (Io.File.Exists(filePath))
            {
                String json = Io.File.ReadAllText(filePath);
                config = JsonConvert.DeserializeObject <TConfig>(json);
            }
            else
            {
                config = Activator.CreateInstance <TConfig>();
                if (saveDefaultConfig)
                {
                    Io.CreateDirectoryForFile(filePath);
                    Io.File.WriteAllText(filePath, JsonConvert.SerializeObject(config));
                }
            }

            return(config);
        }
        /// <inheritdoc />
        public async Task <Entry> SaveEntryAsync(
            EntryModel input,
            UInt32 id = 0,
            Stream newFileContents = null)
        {
            Entry entry;

            if (id > 0)
            {
                entry = await this.GetEntryAsync(id);

                this._mapper.Map(input, entry);
                this._dbContext.Update(entry);
            }
            else
            {
                entry = this._mapper.Map <Entry>(input);
                this._dbContext.Add(entry);
            }
            await this._dbContext.SaveChangesAsync();

            if (input.Type != EntryType.File)
            {
                return(entry);
            }

            var newFileName = String.IsNullOrEmpty(input.FileContent?.FileName)
                                ? entry.FileContent.FileName
                                : $"{entry.Id:D8}_{input.FileContent?.FileName}";
            var oldFileName = entry.FileContent.FileName;
            var newFilePath = SimplerPath.Combine(this._config.FileStorageDir, newFileName);
            var hasOldFile  = !String.IsNullOrEmpty(oldFileName);
            var oldFilePath = hasOldFile
                                ? SimplerPath.Combine(this._config.FileStorageDir, oldFileName)
                                : null;

            hasOldFile = hasOldFile && this._fileSystem.File.Exists(oldFilePath);

            // Rename only
            if (hasOldFile && newFileContents == null && newFileName != oldFileName)
            {
                this._fileSystem.File.Move(oldFilePath, newFilePath);
                try {
                    entry.FileContent.FileName = newFileName;
                    this._dbContext.Update(entry);
                    await this._dbContext.SaveChangesAsync();
                }
                catch (Exception)
                {
                    // In case of errors updating DB, move the file back
                    this._fileSystem.File.Move(newFilePath, oldFilePath);
                    throw;
                }
            }
            // Update file
            else if (newFileContents != null)
            {
                var newFileTempPath =
                    this._fileSystem.Path.GetTempPath() + Guid.NewGuid() + Path.GetExtension(newFileName);
                var oldFileTempPath =
                    this._fileSystem.Path.GetTempPath() + Guid.NewGuid() + Path.GetExtension(oldFileName);

                try
                {
                    // Copy new and current files to temp
                    using (Stream tempFile = this._streams.GetFileStream(newFileTempPath, FileMode.Create))
                    {
                        await newFileContents.CopyToAsync(tempFile);
                    }

                    if (hasOldFile)
                    {
                        File.Copy(oldFilePath, oldFileTempPath);
                    }

                    using (IDbContextTransaction trans = await this._dbContext.Database.BeginTransactionAsync())
                    {
                        try
                        {
                            // Move new file over
                            this._fileSystem.CreateDirectoryForFile(newFilePath);
                            if (hasOldFile && newFileName == oldFileName)
                            {
                                this._fileSystem.File.Delete(oldFilePath);
                            }
                            this._fileSystem.File.Move(newFileTempPath, newFilePath);

                            // Update entry
                            if (entry.FileContent == null)
                            {
                                entry.FileContent = new EntryFileContent();
                            }
                            entry.FileContent.FileName = newFileName;
                            this._dbContext.Entries.Update(entry);
                            await this._dbContext.SaveChangesAsync();

                            trans.Commit();

                            // Delete old file
                            if (hasOldFile && newFileName != oldFileName)
                            {
                                this._fileSystem.File.Delete(oldFilePath);
                            }
                        }
                        catch (Exception)
                        {
                            // If anything fails, move the old file back and cancel DB update
                            if (hasOldFile)
                            {
                                this._fileSystem.File.Move(oldFileTempPath, oldFilePath);
                            }
                            trans.Rollback();
                            throw;
                        }
                    }
                } finally
                {
                    // Delete leftovers
                    this._fileSystem.File.Delete(newFileTempPath);
                    this._fileSystem.File.Delete(oldFileTempPath);
                }
            }

            return(entry);
        }