(filePath, fileEncoding string)
| 1649 | ) |
| 1650 | |
| 1651 | func (h *fsHandler) compressAndOpenFSFile(filePath, fileEncoding string) (*fsFile, error) { |
| 1652 | f, err := h.filesystem.Open(filePath) |
| 1653 | if err != nil { |
| 1654 | return nil, err |
| 1655 | } |
| 1656 | |
| 1657 | fileInfo, err := f.Stat() |
| 1658 | if err != nil { |
| 1659 | _ = f.Close() |
| 1660 | return nil, fmt.Errorf("cannot obtain info for file %q: %w", filePath, err) |
| 1661 | } |
| 1662 | |
| 1663 | if fileInfo.IsDir() { |
| 1664 | _ = f.Close() |
| 1665 | return nil, errDirIndexRequired |
| 1666 | } |
| 1667 | |
| 1668 | if strings.HasSuffix(filePath, h.compressedFileSuffixes[fileEncoding]) || |
| 1669 | fileInfo.Size() > fsMaxCompressibleFileSize || |
| 1670 | !isFileCompressible(f, fsMinCompressRatio) { |
| 1671 | return h.newFSFile(f, fileInfo, false, filePath, "") |
| 1672 | } |
| 1673 | |
| 1674 | compressedFilePath := h.filePathToCompressed(filePath) |
| 1675 | |
| 1676 | if _, ok := h.filesystem.(*osFS); !ok { |
| 1677 | return h.newCompressedFSFileCache(f, fileInfo, compressedFilePath, fileEncoding) |
| 1678 | } |
| 1679 | |
| 1680 | if compressedFilePath != filePath { |
| 1681 | if err := os.MkdirAll(filepath.Dir(compressedFilePath), 0o750); err != nil { |
| 1682 | return nil, err |
| 1683 | } |
| 1684 | } |
| 1685 | compressedFilePath += h.compressedFileSuffixes[fileEncoding] |
| 1686 | |
| 1687 | absPath, err := filepath.Abs(compressedFilePath) |
| 1688 | if err != nil { |
| 1689 | _ = f.Close() |
| 1690 | return nil, fmt.Errorf("cannot determine absolute path for %q: %v", compressedFilePath, err) |
| 1691 | } |
| 1692 | |
| 1693 | flock := acquireFileLock(absPath) |
| 1694 | flock.mu.Lock() |
| 1695 | defer func() { |
| 1696 | flock.mu.Unlock() |
| 1697 | releaseFileLock(absPath, flock) |
| 1698 | }() |
| 1699 | return h.compressFileNolock(f, fileInfo, filePath, compressedFilePath, fileEncoding) |
| 1700 | } |
| 1701 | |
| 1702 | func (h *fsHandler) compressFileNolock( |
| 1703 | f fs.File, fileInfo fs.FileInfo, filePath, compressedFilePath, fileEncoding string, |
no test coverage detected