@@ -68,9 +68,8 @@ type TorrentMakeOptions struct {
6868 MinSize int64
6969 Excludes []string
7070 AllowRestrictedCharInFilename bool
71- // By default, limit filename length to at most 240 bytes (UTF-8).
72- // It's the limit imposed by libtorrent on Linux.
73- AllowLongName bool
71+ // If > 0, limit filename (not full path) length to at most these bytes (UTF-8 string).
72+ FilenameLengthLimit int64
7473}
7574
7675var (
@@ -720,7 +719,7 @@ func MakeTorrent(options *TorrentMakeOptions) (tinfo *TorrentMeta, err error) {
720719 }
721720 log .Infof ("Creating torrent for %q" , options .ContentPath )
722721 if err := infoBuildFromFilePath (info , options .ContentPath , options .Excludes ,
723- options .AllowRestrictedCharInFilename , options .AllowLongName ); err != nil {
722+ options .AllowRestrictedCharInFilename , options .FilenameLengthLimit ); err != nil {
724723 return nil , fmt .Errorf ("failed to build info from content-path: %w" , err )
725724 }
726725 if len (info .Files ) == 0 {
@@ -780,7 +779,7 @@ func MakeTorrent(options *TorrentMakeOptions) (tinfo *TorrentMeta, err error) {
780779// Adapted from metainfo.BuildFromFilePath.
781780// excludes: gitignore style exclude-file-patterns.
782781func infoBuildFromFilePath (info * metainfo.Info , root string , excludes []string ,
783- allowAnyCharInName bool , allowLongName bool ) (err error ) {
782+ allowAnyCharInName bool , filenameLengthLimit int64 ) (err error ) {
784783 info .Name = func () string {
785784 b := filepath .Base (root )
786785 switch b {
@@ -807,7 +806,7 @@ func infoBuildFromFilePath(info *metainfo.Info, root string, excludes []string,
807806 }
808807 }
809808 }
810- if ! allowLongName && len (fi .Name ()) > constants . TORRENT_CONTENT_FILENAME_LENGTH_LIMIT {
809+ if filenameLengthLimit > 0 && int64 ( len (fi .Name ())) > filenameLengthLimit {
811810 return fmt .Errorf ("filename %q is too long (%d bytes in UTF-8). Consider truncate it to %q" , fi .Name (),
812811 len (fi .Name ()), util .StringPrefixInBytes (fi .Name (), constants .TORRENT_CONTENT_FILENAME_LENGTH_LIMIT ))
813812 }
@@ -824,7 +823,8 @@ func infoBuildFromFilePath(info *metainfo.Info, root string, excludes []string,
824823 return fmt .Errorf ("error getting relative path: %s" , err )
825824 }
826825 if ! allowAnyCharInName && constants .FilepathInvalidCharsRegex .MatchString (relPath ) {
827- return fmt .Errorf ("invalid content file path %q: contains restrictive chars" , relPath )
826+ return fmt .Errorf ("invalid content file path %q: contains restrictive chars. Consider rename it to %q" ,
827+ relPath , constants .FilenameRestrictedCharacterReplacer .Replace (relPath ))
828828 }
829829 info .Files = append (info .Files , metainfo.FileInfo {
830830 Path : strings .Split (relPath , string (filepath .Separator )),
0 commit comments