private FileItem CreateFileItem(string directoryPath, string fileName, int size, int attributes, DateTime date)
        {
            var fileEntry = string.Format("{0},{1},{2},{3},{4},{5}",
                                          directoryPath,
                                          fileName,
                                          size,
                                          attributes,
                                          FatDateTime.ConvertFromDateTimeToDateInt(date),
                                          FatDateTime.ConvertFromDateTimeToTimeInt(date));

            return(new FileItem(fileEntry, directoryPath));
        }
        private void TestImportBase(
            string directoryPath,
            string fileName,
            long size,
            bool isFile,
            DateTime date,
            bool isImported = true)
        {
            var fileEntry = string.Format("{0},{1},{2},{3},{4},{5}",
                                          directoryPath,
                                          fileName,
                                          size,
                                          (isFile ? 32 : 16),
                                          FatDateTime.ConvertFromDateTimeToDateInt(date),
                                          FatDateTime.ConvertFromDateTimeToTimeInt(date));

            TestImportBase(fileEntry, directoryPath, directoryPath, fileName, size, date, isImported);
        }
Beispiel #3
0
        private static readonly Regex _asciiPattern = new Regex(@"^[\x20-\x7F]+$", RegexOptions.Compiled); // Pattern for ASCII code (alphanumeric symbols)

        /// <summary>
        /// Imports file entry from a file list in FlashAir card.
        /// </summary>
        /// <param name="fileEntry">File entry from the list</param>
        /// <param name="directoryPath">Remote directory path used to get the list</param>
        /// <returns>True if successfully imported</returns>
        private bool Import(string fileEntry, string directoryPath)
        {
            if (string.IsNullOrWhiteSpace(fileEntry))
            {
                return(false);
            }

            var fileEntryWithoutDirectory = fileEntry.Trim();

            if (!string.IsNullOrWhiteSpace(directoryPath))
            {
                // Check if the leading part of file entry matches directory path. Be aware that the length of
                // file entry like "WLANSD_FILELIST" may be shorter than that of directory path.
                if (!fileEntry.StartsWith(directoryPath, StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }

                Directory = directoryPath;

                // Check if directory path is valid.
                if (!_asciiPattern.IsMatch(Directory) ||                                              // This ASCII checking may be needless because response from FlashAir card seems to be encoded by ASCII.
                    Path.GetInvalidPathChars().Concat(new[] { '?' }).Any(x => Directory.Contains(x))) // '?' appears typically when byte array is not correctly encoded.
                {
                    return(false);
                }

                fileEntryWithoutDirectory = fileEntry.Substring(directoryPath.Length).TrimStart();
            }
            else
            {
                Directory = string.Empty;
            }

            if (!fileEntryWithoutDirectory.ElementAt(0).Equals(_separator))
            {
                return(false);
            }

            var elements = fileEntryWithoutDirectory.TrimStart(_separator)
                           .Split(new[] { _separator }, StringSplitOptions.None)
                           .ToList();

            if (elements.Count < 5)             // 5 means file name, size, raw attribute, raw data and raw time.
            {
                return(false);
            }

            while (elements.Count > 5)             // In the case that file name includes separator character
            {
                elements[0] = $"{elements[0]}{_separator}{elements[1]}";
                elements.RemoveAt(1);
            }

            FileName = elements[0].Trim();

            // Check if file name is valid.
            if (string.IsNullOrWhiteSpace(FileName) ||
                !_asciiPattern.IsMatch(FileName) ||                 // This ASCII checking may be needless because response from FlashAir card seems to be encoded by ASCII.
                Path.GetInvalidFileNameChars().Any(x => FileName.Contains(x)))
            {
                return(false);
            }

            // Determine size, attribute and date.
            int rawDate = 0;
            int rawTime = 0;

            for (int i = 1; i <= 4; i++)
            {
                int num;
                if (!int.TryParse(elements[i], out num))
                {
                    return(false);
                }

                switch (i)
                {
                case 1:
                    // In the case that file size is larger than 2GiB (Int32.MaxValue in bytes), it cannot pass
                    // Int32.TryParse method and so such file will be ignored.
                    Size = num;
                    break;

                case 2:
                    SetAttributes(num);
                    break;

                case 3:
                    rawDate = num;
                    break;

                case 4:
                    rawTime = num;
                    break;
                }
            }

            Date = FatDateTime.ConvertFromDateIntAndTimeIntToDateTime(rawDate, rawTime);

            // Determine file extension.
            if (!IsDirectory && !IsVolume)
            {
                var extension = Path.GetExtension(FileName);
                SetFileExtension(extension);
            }

            return(true);
        }