mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +00:00 
			
		
		
		
	Fix package upload temp path (#34196)
Fix #34195 The temp dir should be created when it is used.
This commit is contained in:
		| @@ -13,9 +13,8 @@ import ( | |||||||
| // Package registry settings | // Package registry settings | ||||||
| var ( | var ( | ||||||
| 	Packages = struct { | 	Packages = struct { | ||||||
| 		Storage           *Storage | 		Storage *Storage | ||||||
| 		Enabled           bool | 		Enabled bool | ||||||
| 		ChunkedUploadPath string |  | ||||||
|  |  | ||||||
| 		LimitTotalOwnerCount int64 | 		LimitTotalOwnerCount int64 | ||||||
| 		LimitTotalOwnerSize  int64 | 		LimitTotalOwnerSize  int64 | ||||||
| @@ -65,13 +64,6 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if HasInstallLock(rootCfg) { |  | ||||||
| 		Packages.ChunkedUploadPath, err = AppDataTempDir("package-upload").MkdirAllSub("") |  | ||||||
| 		if err != nil { |  | ||||||
| 			return fmt.Errorf("unable to create chunked upload directory: %w", err) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	Packages.LimitTotalOwnerSize = mustBytes(sec, "LIMIT_TOTAL_OWNER_SIZE") | 	Packages.LimitTotalOwnerSize = mustBytes(sec, "LIMIT_TOTAL_OWNER_SIZE") | ||||||
| 	Packages.LimitSizeAlpine = mustBytes(sec, "LIMIT_SIZE_ALPINE") | 	Packages.LimitSizeAlpine = mustBytes(sec, "LIMIT_SIZE_ALPINE") | ||||||
| 	Packages.LimitSizeArch = mustBytes(sec, "LIMIT_SIZE_ARCH") | 	Packages.LimitSizeArch = mustBytes(sec, "LIMIT_SIZE_ARCH") | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ import ( | |||||||
| 	packages_model "code.gitea.io/gitea/models/packages" | 	packages_model "code.gitea.io/gitea/models/packages" | ||||||
| 	packages_module "code.gitea.io/gitea/modules/packages" | 	packages_module "code.gitea.io/gitea/modules/packages" | ||||||
| 	"code.gitea.io/gitea/modules/setting" | 	"code.gitea.io/gitea/modules/setting" | ||||||
| 	"code.gitea.io/gitea/modules/util" | 	"code.gitea.io/gitea/modules/tempdir" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -30,8 +30,12 @@ type BlobUploader struct { | |||||||
| 	reading bool | 	reading bool | ||||||
| } | } | ||||||
|  |  | ||||||
| func buildFilePath(id string) string { | func uploadPathTempDir() *tempdir.TempDir { | ||||||
| 	return util.FilePathJoinAbs(setting.Packages.ChunkedUploadPath, id) | 	return setting.AppDataTempDir("package-upload") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func buildFilePath(uploadPath *tempdir.TempDir, id string) string { | ||||||
|  | 	return uploadPath.JoinPath(id) | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewBlobUploader creates a new blob uploader for the given id | // NewBlobUploader creates a new blob uploader for the given id | ||||||
| @@ -48,7 +52,12 @@ func NewBlobUploader(ctx context.Context, id string) (*BlobUploader, error) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	f, err := os.OpenFile(buildFilePath(model.ID), os.O_RDWR|os.O_CREATE, 0o666) | 	uploadPath := uploadPathTempDir() | ||||||
|  | 	_, err = uploadPath.MkdirAllSub("") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	f, err := os.OpenFile(buildFilePath(uploadPath, model.ID), os.O_RDWR|os.O_CREATE, 0o666) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| @@ -118,13 +127,13 @@ func (u *BlobUploader) Read(p []byte) (int, error) { | |||||||
| 	return u.file.Read(p) | 	return u.file.Read(p) | ||||||
| } | } | ||||||
|  |  | ||||||
| // Remove deletes the data and the model of a blob upload | // RemoveBlobUploadByID Remove deletes the data and the model of a blob upload | ||||||
| func RemoveBlobUploadByID(ctx context.Context, id string) error { | func RemoveBlobUploadByID(ctx context.Context, id string) error { | ||||||
| 	if err := packages_model.DeleteBlobUploadByID(ctx, id); err != nil { | 	if err := packages_model.DeleteBlobUploadByID(ctx, id); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err := os.Remove(buildFilePath(id)) | 	err := os.Remove(buildFilePath(uploadPathTempDir(), id)) | ||||||
| 	if err != nil && !os.IsNotExist(err) { | 	if err != nil && !os.IsNotExist(err) { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user