Skip to content

Commit

Permalink
in order to avoid using File.Exists in webgl (which does not work cor…
Browse files Browse the repository at this point in the history
…rectly) filename is now changed to a guid
  • Loading branch information
TomSimons committed Oct 21, 2024
1 parent eade36e commit 30cd1ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
24 changes: 6 additions & 18 deletions Assets/Scripts/Layers/Adapters/DataTypeAdapters/FileTypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,10 @@ public void ProcessFile(string file)

var fileName = Path.GetFileNameWithoutExtension(file);
var extension = Path.GetExtension(file).ToLower();
var newFilePathRelative = Path.Combine(assetsFolderName, fileName + extension);
var newFilePathAbsolute = Path.Combine(assetsFolderPath, fileName + extension);

Debug.Log("checking if file exists: " + newFilePathAbsolute);
Debug.Log("file exists: " + File.Exists(newFilePathAbsolute));

// Find a unique file name if a file with the same name already exists
int index = 0;
while (File.Exists(newFilePathAbsolute))
{
Debug.Log("file already exists: " + newFilePathAbsolute);
index++;
var newFileName = $"{fileName}({index}){extension}";
newFilePathRelative = Path.Combine(assetsFolderName, newFileName);
newFilePathAbsolute = Path.Combine(assetsFolderPath, newFileName);
Debug.Log("attempting to save as: " + newFilePathAbsolute);
}

var guid = new Guid();
var newFilePathRelative = Path.Combine(assetsFolderName, guid + extension);
var newFilePathAbsolute = Path.Combine(assetsFolderPath, guid + extension);

Debug.Log(absoluteFilePath + " will be moved to: " + newFilePathAbsolute);
File.Move(absoluteFilePath, newFilePathAbsolute);
Expand All @@ -80,7 +67,8 @@ public void ProcessFile(string file)
var localFile = new LocalFile()
{
SourceUrl = newFilePathRelative,
LocalFilePath = newFilePathRelative
LocalFilePath = newFilePathRelative,
OriginalFileName = fileName
};

fileTypeEvent.FileReceived.Invoke(localFile);
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Layers/Adapters/IDataTypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class LocalFile
public string SourceUrl;
public string LocalFilePath;

public string OriginalFileName; // in order to avoid overwrites, we change the file name to a uuid, but we want to keep the original name for the user

public List<string> log = new();
}

Expand Down

0 comments on commit 30cd1ef

Please sign in to comment.