Beispiel #1
0
        public Deck Deserialize(FsPath file, FsPath dir)
        {
            State.LastLoadedFile = file;

            Deck deck = Deck.Create();

            deck.File = file;

            int  maxLen = 0x8000000;            // 128 MB
            long length = file.File().Length;

            if (length > maxLen)
            {
                deck.Error = $"File size {length} bytes exceeds maximum of {maxLen} bytes";
                return(deck);
            }

            string serialized;

            try
            {
                serialized = file.ReadAllText();
            }
            catch (IOException ex)
            {
                deck.Error = ex.Message;
                return(deck);
            }

            var format = @"*" + file.Extension();

            var formatter = getFormatter(format, serialized);

            if (formatter == null)
            {
                deck.Error = "Deck format is not supported";
                return(deck);
            }

            deck = LoadSerialized(format, serialized, exact: false);

            deck.File = file;

            if (deck.Name == null)
            {
                string getNestedFileName() =>
                dir.Base().Join(file.RelativeTo(dir)).Value
                .Replace(new string(Path.DirectorySeparatorChar, 1), Environment.NewLine);

                var extension = file.Extension();

                string nameBase = !dir.HasValue()
                                        ? file.Basename()
                                        : getNestedFileName();

                deck.Name = nameBase.Substring(0, nameBase.Length - extension.Length);
            }

            return(deck);
        }
Beispiel #2
0
        public async Task <bool> DownloadAndExtract(string remotePath, FsPath targetDirectory, FsPath fileName, CancellationToken token)
        {
            if (!Str.Equals(".7z", fileName.Extension()))
            {
                throw new ArgumentException();
            }

            FsPath archiveFileName = targetDirectory.Join(fileName);

            if (archiveFileName.IsFile())
            {
                try
                {
                    archiveFileName.DeleteFile();
                }
                catch (Exception ex)
                {
                    lock (_syncOutput)
                        Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
                    return(false);
                }
            }

            bool downloaded = await TryDownloadFile(remotePath, archiveFileName, token);

            if (!downloaded)
            {
                return(false);
            }

            if (!archiveFileName.IsFile())
            {
                lock (_syncOutput)
                    Console.WriteLine($"Failed to download {archiveFileName} from {remotePath}");
                return(false);
            }

            var sevenZip = new SevenZip(silent: true);

            sevenZip.Extract(archiveFileName, targetDirectory, Enumerable.Empty <FsPath>());

            try
            {
                archiveFileName.DeleteFile();
            }
            catch (Exception ex)
            {
                lock (_syncOutput)
                    Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
            }

            return(true);
        }