fix(project background): add more checks for whether a background file exists when duplicating or deleting a project
continuous-integration/drone/push Build is failing Details

Related discussion: https://community.vikunja.io/t/500-internal-server-error-when-selecting-unsplash-background-image/778/18
This commit is contained in:
kolaente 2023-09-13 11:20:40 +02:00
parent 191c154150
commit 1ee243f2bd
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 63 additions and 41 deletions

View File

@ -1030,7 +1030,12 @@ func (p *Project) DeleteBackgroundFileIfExists() (err error) {
}
file := files.File{ID: p.BackgroundFileID}
return file.Delete()
err = file.Delete()
if err != nil && files.IsErrFileDoesNotExist(err) {
return nil
}
return err
}
// SetProjectBackground sets a background file as project background in the db

View File

@ -118,43 +118,9 @@ func (pd *ProjectDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
return
}
// Background files + unsplash info
if pd.Project.BackgroundFileID != 0 {
log.Debugf("Duplicating background %d from project %d into %d", pd.Project.BackgroundFileID, pd.ProjectID, pd.Project.ID)
f := &files.File{ID: pd.Project.BackgroundFileID}
if err := f.LoadFileMetaByID(); err != nil {
return err
}
if err := f.LoadFileByID(); err != nil {
return err
}
defer f.File.Close()
file, err := files.Create(f.File, f.Name, f.Size, doer)
if err != nil {
return err
}
// Get unsplash info if applicable
up, err := GetUnsplashPhotoByFileID(s, pd.Project.BackgroundFileID)
if err != nil && files.IsErrFileIsNotUnsplashFile(err) {
return err
}
if up != nil {
up.ID = 0
up.FileID = file.ID
if err := up.Save(s); err != nil {
return err
}
}
if err := SetProjectBackground(s, pd.Project.ID, file, pd.Project.BackgroundBlurHash); err != nil {
return err
}
log.Debugf("Duplicated project background from project %d into %d", pd.ProjectID, pd.Project.ID)
err = duplicateProjectBackground(s, pd, doer)
if err != nil {
return
}
// Rights / Shares
@ -207,6 +173,54 @@ func (pd *ProjectDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
return
}
func duplicateProjectBackground(s *xorm.Session, pd *ProjectDuplicate, doer web.Auth) (err error) {
if pd.Project.BackgroundFileID == 0 {
return
}
log.Debugf("Duplicating background %d from project %d into %d", pd.Project.BackgroundFileID, pd.ProjectID, pd.Project.ID)
f := &files.File{ID: pd.Project.BackgroundFileID}
err = f.LoadFileMetaByID()
if err != nil && files.IsErrFileDoesNotExist(err) {
pd.Project.BackgroundFileID = 0
return nil
}
if err != nil {
return err
}
if err := f.LoadFileByID(); err != nil {
return err
}
defer f.File.Close()
file, err := files.Create(f.File, f.Name, f.Size, doer)
if err != nil {
return err
}
// Get unsplash info if applicable
up, err := GetUnsplashPhotoByFileID(s, pd.Project.BackgroundFileID)
if err != nil && !files.IsErrFileIsNotUnsplashFile(err) {
return err
}
if up != nil {
up.ID = 0
up.FileID = file.ID
if err := up.Save(s); err != nil {
return err
}
}
if err := SetProjectBackground(s, pd.Project.ID, file, pd.Project.BackgroundBlurHash); err != nil {
return err
}
log.Debugf("Duplicated project background from project %d into %d", pd.ProjectID, pd.Project.ID)
return
}
func duplicateTasks(s *xorm.Session, doer web.Auth, ld *ProjectDuplicate, bucketMap map[int64]int64) (err error) {
// Get all tasks + all task details
tasks, _, _, err := getTasksForProjects(s, []*Project{{ID: ld.ProjectID}}, doer, &taskSearchOptions{})

View File

@ -291,11 +291,13 @@ func (p *Provider) Set(s *xorm.Session, image *background.Image, project *models
// Remove the old background if one exists
if project.BackgroundFileID != 0 {
file := files.File{ID: project.BackgroundFileID}
if err := file.Delete(); err != nil {
err = file.Delete()
if err != nil && !files.IsErrFileDoesNotExist(err) {
return err
}
if err := models.RemoveUnsplashPhoto(s, project.BackgroundFileID); err != nil {
err = models.RemoveUnsplashPhoto(s, project.BackgroundFileID)
if err != nil && !files.IsErrFileDoesNotExist(err) {
return err
}
}

View File

@ -56,7 +56,8 @@ func (p *Provider) Set(s *xorm.Session, img *background.Image, project *models.P
// Remove the old background if one exists
if project.BackgroundFileID != 0 {
file := files.File{ID: project.BackgroundFileID}
if err := file.Delete(); err != nil {
err := file.Delete()
if err != nil && !files.IsErrFileDoesNotExist(err) {
return err
}
}