5
5
"strconv"
6
6
"strings"
7
7
"sync"
8
+ "unicode/utf8"
8
9
9
10
"github.com/mmcdole/gofeed"
10
11
log "github.com/sirupsen/logrus"
@@ -19,6 +20,28 @@ type Feed struct {
19
20
var wg sync.WaitGroup
20
21
var isAllUpdate bool
21
22
23
+ /*
24
+ Based on the table in https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
25
+
26
+ the majority of filesystems have a limit of 255.
27
+
28
+ Some of them refer to "bytes" and others refer to "UTF-8 characters".
29
+ Ideally we'd like to take as much as that as possible but we run the risk of
30
+ truncating at a point which leaves us with an incomplete UTF8 code point
31
+ representation. Instead, we need a UTF8-safe truncate - we define that function below.
32
+ */
33
+ const maxFileNameLength = 255
34
+
35
+ func truncateString (s string , n int ) string {
36
+ if len (s ) <= n {
37
+ return s
38
+ }
39
+ for ! utf8 .ValidString (s [:n ]) {
40
+ n --
41
+ }
42
+ return s [:n ]
43
+ }
44
+
22
45
func DeleteFeedFiles (name string ) {
23
46
os .RemoveAll (Config .FeedDirectory + "/" + name )
24
47
os .MkdirAll (Config .FeedDirectory + "/" + name , 0777 )
@@ -37,7 +60,7 @@ func UpdateFeed(name string) {
37
60
}
38
61
DeleteFeedFiles (name )
39
62
for _ , item := range feed .Items {
40
- file , err := os .Create (Config .FeedDirectory + "/" + name + "/" + strings .ReplaceAll (item .Title , "/" , "" ))
63
+ file , err := os .Create (Config .FeedDirectory + "/" + name + "/" + truncateString ( strings .ReplaceAll (item .Title , "/" , "" ), maxFileNameLength ))
41
64
if err != nil {
42
65
log .Error ("Failed to create a file for article titled '" + item .Title + "'" )
43
66
continue
0 commit comments