Skip to content

Commit

Permalink
Adding support for Metadata External Url and allowing to use or not .…
Browse files Browse the repository at this point in the history
…json file extension
  • Loading branch information
ptedeschi committed Feb 18, 2022
1 parent c7033fa commit ceb765c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 32 deletions.
24 changes: 24 additions & 0 deletions src/NFT.net/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/NFT.net/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
<Setting Name="CollectionImageNamePrefix" Type="System.String" Scope="User">
<Value Profile="(Default)">nft #</Value>
</Setting>
<Setting Name="MetadataExternalUrl" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="MetadataUseFileExtension" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
5 changes: 3 additions & 2 deletions src/NFT.net/Services/Collection/CollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CollectionService(ILayerService layerService, IGeneratorService generator

public event EventHandler<ImageEventArgs> CollectionItemStatus;

public void Create(string layersFolder, string outputFolder, int metadataType, string metadataDescription, string metadataImageBaseUri, int collectionSize, int collectionInitialNumber, string collectionImagePrefix)
public void Create(string layersFolder, string outputFolder, int metadataType, string metadataDescription, string metadataImageBaseUri, string metadataExternalUrl, bool metadataUseFileExtension, int collectionSize, int collectionInitialNumber, string collectionImagePrefix)
{
var layers = this.layerService.Load(layersFolder);

Expand Down Expand Up @@ -69,6 +69,7 @@ public void Create(string layersFolder, string outputFolder, int metadataType, s
Filename = Uri.EscapeDataString(filename),
Description = metadataDescription,
Image = $"{metadataImageBaseUri}/{Uri.EscapeDataString(filename)}",
ExternalUrl = metadataExternalUrl,
Attributes = item.Attributes.Select(i => new Model.Attribute { Layer = i.Layer, Value = i.Value }).ToList(),
};

Expand All @@ -89,7 +90,7 @@ public void Create(string layersFolder, string outputFolder, int metadataType, s
collectionNumber++;
}

this.metadataService.Generate(outputFolder, metadataList, metadataType);
this.metadataService.Generate(outputFolder, metadataList, metadataType, metadataUseFileExtension);

this.rarityService.Generate(outputFolder, metadataList, metadataType);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NFT.net/Services/Collection/ICollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface ICollectionService
{
event EventHandler<ImageEventArgs> CollectionItemStatus;

void Create(string layersFolder, string outputFolder, int metadataType, string metadataDescription, string metadataImageBaseUri, int collectionSize, int collectionInitialNumber, string collectionImagePrefix);
void Create(string layersFolder, string outputFolder, int metadataType, string metadataDescription, string metadataImageBaseUri, string metadataExternalUrl, bool metadataUseFileExtension, int collectionSize, int collectionInitialNumber, string collectionImagePrefix);
}
}
2 changes: 1 addition & 1 deletion src/NFT.net/Services/Generator/GeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public List<ImageDescriptor> Create(List<Layer> layers, int collectionSize, stri
// Load predefined images (if available)
var presetDna = this.GetPresetDna(layersFolder);

if (presetDna != null && presetDna.Preset != null && presetDna.Preset.Length > 0)
if (presetDna != null && presetDna.Preset != null && presetDna.Preset.Length > 0 && presetDna.Preset.Length < collectionSize)
{
foreach (var dna in presetDna.Preset)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NFT.net/Services/Metadata/IMetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Tedeschi.NFT.Services.Metadata

public interface IMetadataService
{
void Generate(string outputFolder, List<Metadata> metadataList, int type);
void Generate(string outputFolder, List<Metadata> metadataList, int type, bool useFileExtension);

void Update(string outputFolder, string newImageBaseUri, int type);
void Update(string outputFolder, string newImageBaseUri, int type, bool useFileExtension);
}
}
19 changes: 11 additions & 8 deletions src/NFT.net/Services/Metadata/MetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ namespace Tedeschi.NFT.Services.Metadata

internal class MetadataService : IMetadataService
{
public void Generate(string outputFolder, List<Metadata> metadataList, int type)
public void Generate(string outputFolder, List<Metadata> metadataList, int type, bool useFileExtension)
{
var metadataLocation = $"{outputFolder}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.FolderName}";
var extension = useFileExtension == true ? Constants.FileExtension.Json : string.Empty;

var serializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Expand All @@ -31,7 +33,7 @@ public void Generate(string outputFolder, List<Metadata> metadataList, int type)
}

var jsonMerged = JsonConvert.SerializeObject(metadataList, Formatting.Indented, serializerSettings);
File.WriteAllText($"{metadataLocation}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.MergedFilename}{Constants.FileExtension.Json}", jsonMerged);
File.WriteAllText($"{metadataLocation}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.MergedFilename}{extension}", jsonMerged);
break;

case Constants.MetadataType.Individual:
Expand All @@ -43,16 +45,17 @@ public void Generate(string outputFolder, List<Metadata> metadataList, int type)
foreach (var metadata in metadataList)
{
var jsonIndividual = JsonConvert.SerializeObject(metadata, Formatting.Indented, serializerSettings);
File.WriteAllText($"{metadataLocation}{Path.DirectorySeparatorChar}{metadata.Id}{Constants.FileExtension.Json}", jsonIndividual);
File.WriteAllText($"{metadataLocation}{Path.DirectorySeparatorChar}{metadata.Id}{extension}", jsonIndividual);
}

break;
}
}

public void Update(string outputFolder, string newImageBaseUri, int type)
public void Update(string outputFolder, string newImageBaseUri, int type, bool useFileExtension)
{
var metadataLocation = $"{outputFolder}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.FolderName}";
var extension = useFileExtension == true ? Constants.FileExtension.Json : string.Empty;

switch (type)
{
Expand All @@ -61,15 +64,15 @@ public void Update(string outputFolder, string newImageBaseUri, int type)

case Constants.MetadataType.Merged:
{
var filename = $"{metadataLocation}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.MergedFilename}{Constants.FileExtension.Json}";
var filename = $"{metadataLocation}{Path.DirectorySeparatorChar}{Constants.MetadataDefault.MergedFilename}{extension}";
var list = JsonConvert.DeserializeObject<List<Metadata>>(File.ReadAllText(filename));

foreach (var item in list)
{
item.Image = $"{newImageBaseUri}/{item.Filename}";
}

this.Generate(outputFolder, list, type);
this.Generate(outputFolder, list, type, useFileExtension);
}

break;
Expand All @@ -78,15 +81,15 @@ public void Update(string outputFolder, string newImageBaseUri, int type)
{
var list = new List<Metadata>();

foreach (string filename in Directory.GetFiles(metadataLocation, $"*{Constants.FileExtension.Json}"))
foreach (string filename in Directory.GetFiles(metadataLocation, $"*{extension}"))
{
var metadata = JsonConvert.DeserializeObject<Metadata>(File.ReadAllText(filename));
metadata.Image = $"{newImageBaseUri}/{metadata.Filename}";

list.Add(metadata);
}

this.Generate(outputFolder, list, type);
this.Generate(outputFolder, list, type, useFileExtension);
}

break;
Expand Down
67 changes: 51 additions & 16 deletions src/NFT.net/View/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ceb765c

Please sign in to comment.