@@ -53,6 +53,12 @@ type sonarrEvent struct {
5353 Series struct {
5454 Path string
5555 } `json:"series"`
56+
57+ RenamedFiles []struct {
58+ // use PreviousPath as the Series.Path might have changed.
59+ PreviousPath string
60+ RelativePath string
61+ } `json:"renamedEpisodeFiles"`
5662}
5763
5864func (h handler ) ServeHTTP (rw http.ResponseWriter , r * http.Request ) {
@@ -70,37 +76,93 @@ func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
7076 rlog .Trace ().Interface ("event" , event ).Msg ("Received JSON body" )
7177
7278 if strings .EqualFold (event .Type , "Test" ) {
73- rlog .Debug ().Msg ("Received test event" )
79+ rlog .Info ().Msg ("Received test event" )
7480 rw .WriteHeader (http .StatusOK )
7581 return
7682 }
7783
78- if ! strings .EqualFold (event .Type , "Download" ) || event .File .RelativePath == "" || event .Series .Path == "" {
79- rlog .Error ().Msg ("Required fields are missing" )
80- rw .WriteHeader (http .StatusBadRequest )
81- return
84+ var paths []string
85+
86+ // a Download event is either an upgrade or a new file.
87+ // the EpisodeFileDelete event shares the same request format as Download.
88+ if strings .EqualFold (event .Type , "Download" ) || strings .EqualFold (event .Type , "EpisodeFileDelete" ) {
89+ if event .File .RelativePath == "" || event .Series .Path == "" {
90+ rlog .Error ().Msg ("Required fields are missing" )
91+ return
92+ }
93+
94+ // Use path.Dir to get the directory in which the file is located
95+ folderPath := path .Dir (path .Join (event .Series .Path , event .File .RelativePath ))
96+ paths = append (paths , folderPath )
97+ }
98+
99+ // An entire show has been deleted
100+ if strings .EqualFold (event .Type , "SeriesDelete" ) {
101+ if event .Series .Path == "" {
102+ rlog .Error ().Msg ("Required fields are missing" )
103+ return
104+ }
105+
106+ // Scan the folder of the show
107+ paths = append (paths , event .Series .Path )
108+ }
109+
110+ if strings .EqualFold (event .Type , "Rename" ) {
111+ if event .Series .Path == "" {
112+ rlog .Error ().Msg ("Required fields are missing" )
113+ return
114+ }
115+
116+ // Keep track of which paths we have already added to paths.
117+ encountered := make (map [string ]bool )
118+
119+ for _ , renamedFile := range event .RenamedFiles {
120+ previousPath := path .Dir (renamedFile .PreviousPath )
121+ currentPath := path .Dir (path .Join (event .Series .Path , renamedFile .RelativePath ))
122+
123+ // if previousPath not in paths, then add it.
124+ if _ , ok := encountered [previousPath ]; ! ok {
125+ encountered [previousPath ] = true
126+ paths = append (paths , previousPath )
127+ }
128+
129+ // if currentPath not in paths, then add it.
130+ if _ , ok := encountered [currentPath ]; ! ok {
131+ encountered [currentPath ] = true
132+ paths = append (paths , currentPath )
133+ }
134+ }
82135 }
83136
84- // Rewrite the path based on the provided rewriter.
85- folderPath := path .Dir (h .rewrite (path .Join (event .Series .Path , event .File .RelativePath )))
137+ var scans []autoscan.Scan
138+
139+ for _ , folderPath := range paths {
140+ folderPath := h .rewrite (folderPath )
141+
142+ scan := autoscan.Scan {
143+ Folder : folderPath ,
144+ Priority : h .priority ,
145+ Time : now (),
146+ }
86147
87- scan := autoscan.Scan {
88- Folder : folderPath ,
89- Priority : h .priority ,
90- Time : now (),
148+ scans = append (scans , scan )
91149 }
92150
93- err = h .callback (scan )
151+ err = h .callback (scans ... )
94152 if err != nil {
95- rlog .Error ().Err (err ).Msg ("Processor could not process scan " )
153+ rlog .Error ().Err (err ).Msg ("Processor could not process scans " )
96154 rw .WriteHeader (http .StatusInternalServerError )
97155 return
98156 }
99157
158+ for _ , scan := range scans {
159+ rlog .Info ().
160+ Str ("path" , scan .Folder ).
161+ Str ("event" , event .Type ).
162+ Msg ("Scan moved to processor" )
163+ }
164+
100165 rw .WriteHeader (http .StatusOK )
101- rlog .Info ().
102- Str ("path" , folderPath ).
103- Msg ("Scan moved to processor" )
104166}
105167
106168var now = time .Now
0 commit comments