@@ -71,8 +71,18 @@ export class ProgramController extends LessonsBaseController {
7171 program . churchId = au . churchId ;
7272 const p = program ;
7373 const saveFunction = async ( ) => {
74- if ( p . image && p . image . startsWith ( "data:image/" ) ) await this . saveImage ( p ) ;
75- return await this . repositories . program . save ( p ) ;
74+ // Save the program first so it has an id, then upload the image
75+ // keyed on that id. (Previously the image was uploaded before the
76+ // id existed, so every upload landed at programs/undefined.png.)
77+ const dataUrlPending = p . image && p . image . startsWith ( "data:image/" ) ? p . image : null ;
78+ if ( dataUrlPending ) p . image = "" ;
79+ const saved = await this . repositories . program . save ( p ) ;
80+ if ( dataUrlPending ) {
81+ saved . image = dataUrlPending ;
82+ await this . saveImage ( saved ) ;
83+ await this . repositories . program . save ( saved ) ;
84+ }
85+ return saved ;
7686 } ;
7787 promises . push ( saveFunction ( ) ) ;
7888 } ) ;
@@ -91,7 +101,12 @@ export class ProgramController extends LessonsBaseController {
91101 const resources = await this . repositories . resource . loadByContentTypeId ( au . churchId , "program" , id ) ;
92102 const studies = await this . repositories . study . loadByProgramId ( au . churchId , id ) ;
93103 if ( resources . length > 0 || studies . length > 0 ) return this . json ( { } , 401 ) ;
94- else return await this . repositories . program . delete ( au . churchId , id ) ;
104+ else {
105+ await this . repositories . program . delete ( au . churchId , id ) ;
106+ // Clean up the uploaded image file (if any) so disk doesn't leak.
107+ await FileStorageHelper . remove ( "/programs/" + id + ".png" ) . catch ( ( ) => { } ) ;
108+ return { id } ;
109+ }
95110 }
96111 } ) ;
97112 }
0 commit comments