@@ -37,6 +37,7 @@ public class UploadsController : ApiController
3737
3838 private readonly Lazy < IProductService > _productService ;
3939 private readonly Lazy < IMediaService > _mediaService ;
40+ private readonly Lazy < IFolderService > _folderService ;
4041 private readonly Lazy < IImportProfileService > _importProfileService ;
4142 private readonly Lazy < IStoreService > _storeService ;
4243 private readonly Lazy < IPermissionService > _permissionService ;
@@ -48,6 +49,7 @@ public class UploadsController : ApiController
4849 public UploadsController (
4950 Lazy < IProductService > productService ,
5051 Lazy < IMediaService > mediaService ,
52+ Lazy < IFolderService > folderService ,
5153 Lazy < IImportProfileService > importProfileService ,
5254 Lazy < IStoreService > storeService ,
5355 Lazy < IPermissionService > permissionService ,
@@ -58,6 +60,7 @@ public UploadsController(
5860 {
5961 _productService = productService ;
6062 _mediaService = mediaService ;
63+ _folderService = folderService ;
6164 _importProfileService = importProfileService ;
6265 _storeService = storeService ;
6366 _permissionService = permissionService ;
@@ -168,29 +171,41 @@ await this.ProcessEntityAsync(async () =>
168171 }
169172 else
170173 {
171- if ( ! _mediaService . Value . FindEqualFile ( stream , files , true , out var equalFile ) )
174+ // If TinyImage compresses the image, FindEqualFile will never find an equal image here.
175+ // We have to live with that. There is no ad-hoc solution for it.
176+ if ( _mediaService . Value . FindEqualFile ( stream , files , true , out var equalFile ) )
172177 {
173- var path = _mediaService . Value . CombinePaths ( SystemAlbumProvider . Catalog , image . FileName ) ;
174- var newFile = await _mediaService . Value . SaveFileAsync ( path , stream , false , DuplicateFileHandling . Rename ) ;
178+ image . Exists = true ;
179+ image . Picture = equalFile ;
180+ }
181+ else
182+ {
183+ var catalogAlbumId = _folderService . Value . GetNodeByPath ( SystemAlbumProvider . Catalog ) . Value . Id ;
175184
176- if ( ( newFile ? . Id ?? 0 ) != 0 )
185+ if ( _mediaService . Value . FindEqualFile ( stream , image . FileName , catalogAlbumId , true , out equalFile ) )
177186 {
178- _productService . Value . InsertProductPicture ( new ProductMediaFile
179- {
180- MediaFileId = newFile . Id ,
181- ProductId = entity . Id ,
182- DisplayOrder = ++ displayOrder
183- } ) ;
187+ image . Exists = true ;
188+ image . Picture = equalFile ;
189+ }
190+ else
191+ {
192+ var path = _mediaService . Value . CombinePaths ( SystemAlbumProvider . Catalog , image . FileName ) ;
193+ var newFile = await _mediaService . Value . SaveFileAsync ( path , stream , false , DuplicateFileHandling . Rename ) ;
184194
185- image . Inserted = true ;
186- image . Picture = newFile . File ;
195+ if ( newFile ? . Id > 0 )
196+ {
197+ _productService . Value . InsertProductPicture ( new ProductMediaFile
198+ {
199+ MediaFileId = newFile . Id ,
200+ ProductId = entity . Id ,
201+ DisplayOrder = ++ displayOrder
202+ } ) ;
203+
204+ image . Inserted = true ;
205+ image . Picture = newFile . File ;
206+ }
187207 }
188208 }
189- else
190- {
191- image . Exists = true ;
192- image . Picture = equalFile ;
193- }
194209 }
195210
196211 if ( image . Picture != null )
0 commit comments