@@ -13,6 +13,8 @@ namespace NuGetGallery
1313{
1414 public class CorePackageFileService : ICorePackageFileService
1515 {
16+ private const string LicenseFileName = "license" ;
17+
1618 private readonly ICoreFileStorageService _fileStorageService ;
1719 private readonly IFileMetadataService _metadata ;
1820
@@ -199,6 +201,76 @@ public async Task StorePackageFileInBackupLocationAsync(Package package, Stream
199201 }
200202 }
201203
204+ public Task SaveLicenseFileAsync ( Package package , Stream licenseFile )
205+ {
206+ if ( package == null )
207+ {
208+ throw new ArgumentNullException ( nameof ( package ) ) ;
209+ }
210+
211+ if ( licenseFile == null )
212+ {
213+ throw new ArgumentNullException ( nameof ( licenseFile ) ) ;
214+ }
215+
216+ if ( package . EmbeddedLicenseType == EmbeddedLicenseFileType . Absent )
217+ {
218+ throw new ArgumentException ( "Package must have an embedded license" , nameof ( package ) ) ;
219+ }
220+
221+ var fileName = BuildLicenseFileName ( package ) ;
222+
223+ // Gallery will generally ignore the content type on license files and will use the value from the DB,
224+ // but we'll be nice and try to specify correct content type for them.
225+ var contentType = package . EmbeddedLicenseType == EmbeddedLicenseFileType . Markdown
226+ ? CoreConstants . MarkdownContentType
227+ : CoreConstants . TextContentType ;
228+
229+ return _fileStorageService . SaveFileAsync ( _metadata . PackageContentFolderName , fileName , contentType , licenseFile , overwrite : true ) ;
230+ }
231+
232+ public Task < Stream > DownloadLicenseFileAsync ( Package package )
233+ {
234+ var fileName = BuildLicenseFileName ( package ) ;
235+ return _fileStorageService . GetFileAsync ( _metadata . PackageContentFolderName , fileName ) ;
236+ }
237+
238+ public Task DeleteLicenseFileAsync ( string id , string version )
239+ {
240+ if ( id == null )
241+ {
242+ throw new ArgumentNullException ( nameof ( id ) ) ;
243+ }
244+
245+ if ( string . IsNullOrWhiteSpace ( id ) )
246+ {
247+ throw new ArgumentException ( $ "{ nameof ( id ) } cannot be empty", nameof ( id ) ) ;
248+ }
249+
250+ if ( version == null )
251+ {
252+ throw new ArgumentNullException ( nameof ( version ) ) ;
253+ }
254+
255+ if ( string . IsNullOrWhiteSpace ( version ) )
256+ {
257+ throw new ArgumentException ( $ "{ nameof ( version ) } cannot be empty", nameof ( version ) ) ;
258+ }
259+
260+ var normalizedVersion = NuGetVersionFormatter . Normalize ( version ) ;
261+ var fileName = BuildLicenseFileName ( id , normalizedVersion ) ;
262+
263+ return _fileStorageService . DeleteFileAsync ( _metadata . PackageContentFolderName , fileName ) ;
264+ }
265+
266+ private string LicensePathTemplate => $ "{ _metadata . PackageContentPathTemplate } /{ LicenseFileName } ";
267+
268+ private string BuildLicenseFileName ( Package package )
269+ => BuildFileName ( package , LicensePathTemplate , string . Empty ) ;
270+
271+ private string BuildLicenseFileName ( string id , string version )
272+ => BuildFileName ( id , version , LicensePathTemplate , string . Empty ) ;
273+
202274 private static string BuildBackupFileName ( string id , string version , string hash , string extension , string fileBackupSavePathTemplate )
203275 {
204276 if ( id == null )
0 commit comments