public void PutFile(CacheIndex index, int fileId, CacheFile file)
        {
            // Throw an exception if the output directory is not yet set or does not exist.
            if (string.IsNullOrWhiteSpace(this.BaseDirectory))
            {
                throw new InvalidOperationException("Base directory must be set before writing files.");
            }

            if (file.Data.Length == 0)
            {
                throw new ArgumentException("You cannot put an empty file into a FlatFileCache.");
            }

            var indexDirectory = this.GetIndexDirectory(index);

            // Create index directory when it does not exist yet.
            Directory.CreateDirectory(indexDirectory);

            // Clean existing files (to make sure no variants with different extensions exist).
            foreach (var existingFilePath in this.GetExistingFilePaths(index, fileId))
            {
                if (!this.OverwriteFiles)
                {
                    Log.Debug($"{(int)index}/{fileId} already exists.");
                    return;
                }

                Log.Debug($"Deleting existing {existingFilePath}.");
                System.IO.File.Delete(existingFilePath);
            }

            string?extension;

            if (file.Info.HasEntries)
            {
                // TODO: Add an option to extract entries individually (which generates tons of files but is more useful).
                extension = "entries";
            }
            else
            {
                extension = ExtensionGuesser.GuessExtension(file.Data);
            }
            extension = extension != null ? $".{extension}" : "";

            var filePath = $"{indexDirectory}{fileId}{extension}";

            System.IO.File.WriteAllBytes(filePath, file.Data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes a file to the cache.
        /// The file's info will be used to determine where and how to put the file in the cache.
        /// </summary>
        /// <param name="file"></param>
        /// <exception cref="ArgumentException"></exception>
        public void PutFile(CacheFile file)
        {
            if (file.Info.Index == Index.Undefined || file.Info.FileId == null)
            {
                throw new ArgumentException("A file must have an index and file id to be written.");
            }

            // TODO: #57
            if (file.Info.EntryId != null)
            {
                throw new ArgumentException("Entries can not be directly written to the cache. Use an entry file containing entries or remove the entry id from its info.");
            }

            var binaryFile = file.ToBinaryFile();

            this.PutBinaryFile(binaryFile);
            this.PutFileInfo(binaryFile.Info);
        }
Exemplo n.º 3
0
        public void PutFile(CacheIndex index, int fileId, CacheFile file)
        {
            if (index == CacheIndex.ReferenceTables)
            {
                throw new ArgumentException("Manually writing files to the reference table index is not allowed.");
            }

            var info = file.Info.Clone();

            this.PutFileData(index, fileId, this.FileDecoder.EncodeFile(file, info), info);

            // Write updated reference table.
            var referenceTable = this.GetReferenceTable(index, true);

            referenceTable.SetFileInfo(fileId, info);
            var referenceTableFile = new CacheFile(referenceTable.Encode());

            this.PutFileData(
                CacheIndex.ReferenceTables,
                (int)index,
                this.FileDecoder.EncodeFile(referenceTableFile, null),
                null
                );
        }