Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploaded files are now moved to an assets folder and are not overwrit… #294

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions Assets/Scripts/Layers/Adapters/DataTypeAdapters/FileTypeAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Netherlands3D.Events;
using Netherlands3D.Twin.UI.LayerInspector;
using UnityEngine;
using UnityEngine.Events;

Expand All @@ -16,10 +13,11 @@ public class FileTypeEvent
public string Extension;
public UnityEvent<LocalFile> FileReceived;
}

[CreateAssetMenu(menuName = "Netherlands3D/Adapters/FileTypeAdapter", fileName = "FileTypeAdapter", order = 0)]
public class FileTypeAdapter : ScriptableObject
{
// [SerializeField] private string assetsFolderName = "Assets";
[SerializeField] private List<FileTypeEvent> fileTypeEvents;

public void ProcessFiles(string files)
Expand All @@ -35,29 +33,55 @@ public void ProcessFile(string file)
{
if (file.EndsWith(','))
file = file.Remove(file.Length - 1);

var absoluteFilePath = Path.Combine(Application.persistentDataPath, file);

var fileName = Path.GetFileNameWithoutExtension(file);
var extension = Path.GetExtension(file).ToLower();

var guid = Guid.NewGuid();
var newFilePathRelative = guid + extension;
var newFilePathAbsolute = Path.Combine(Application.persistentDataPath, guid + extension);

// var newFilePathRelative = Path.Combine(fileName + extension);
using (FileStream fileStream = File.Create(newFilePathAbsolute))
{
// byte[] bytes = new byte[] { 0, 1, 2, 3, 5 };
// fileStream.Write(bytes);

string fileExtension = Path.GetExtension(file).ToLower();
if (fileExtension.StartsWith('.'))
fileExtension = fileExtension.Substring(1);

var fileTypeEvent = fileTypeEvents.FirstOrDefault(fte => fte.Extension == fileExtension);

Debug.Log("file: " + file);

if(fileTypeEvent != null)
Debug.Log(absoluteFilePath + " will be copied to: " + newFilePathAbsolute);
var fileBytes = File.ReadAllBytes(absoluteFilePath);
// var fileStream = File.Create(newFilePathAbsolute);
Debug.Log("read: " + fileBytes.Length + " bytes");
fileStream.Write(fileBytes, 0, fileBytes.Length);
Debug.Log("writing: " + fileBytes.Length + " bytes");
File.WriteAllBytes(newFilePathAbsolute, fileBytes);
// File.Copy(absoluteFilePath, newFilePathAbsolute);
}

if (extension.StartsWith('.'))
extension = extension.Substring(1);

var fileTypeEvent = fileTypeEvents.FirstOrDefault(fte => fte.Extension.ToLower() == extension);

Debug.Log("processing file: " + newFilePathRelative);

if (fileTypeEvent != null)
{
var localFile = new LocalFile()
{
SourceUrl = file,
LocalFilePath = file
SourceUrl = newFilePathRelative,
LocalFilePath = newFilePathRelative,
OriginalFileName = fileName
};


fileTypeEvent.FileReceived.Invoke(localFile);
}
else
{
Debug.Log("file type {" + fileExtension + "} does not have an associated processing function");
Debug.Log("file type {" + extension + "} does not have an associated processing function");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public void ParseGeoJSON(LocalFile localFile)

private void CreateGeoJSONLayer(LocalFile localFile, UnityEvent<string> onErrorCallback = null)
{
var localFilePath = Path.Combine(Application.persistentDataPath, localFile.LocalFilePath);
var geoJsonLayerName = Path.GetFileName(localFile.SourceUrl);
if(localFile.SourceUrl.Length > 0)
var geoJsonLayerName = localFile.OriginalFileName;
if(string.IsNullOrEmpty(geoJsonLayerName) && localFile.SourceUrl.Length > 0)
geoJsonLayerName = localFile.SourceUrl;

//Create a new geojson layer with random color (untill UI provides ways to choose colors)
Expand All @@ -71,7 +70,6 @@ private void CreateGeoJSONLayer(LocalFile localFile, UnityEvent<string> onErrorC
newLayer.OnParseError.AddListener(onErrorCallback.Invoke);

var localPath = localFile.LocalFilePath;
var fileName = Path.GetFileName(localPath);
var propertyData = newLayer.PropertyData as LayerURLPropertyData;

if (localFile.SourceUrl.StartsWith("http"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public bool Supports(LocalFile localFile)
public void Execute(LocalFile localFile)
{
var fullPath = localFile.LocalFilePath;
var fileName = Path.GetFileName(fullPath);
ObjSpawner newLayer = Instantiate(layerPrefab);
newLayer.gameObject.name = fileName;
newLayer.gameObject.name = localFile.OriginalFileName;

var propertyData = newLayer.PropertyData as ObjPropertyData;
propertyData.ObjFile = AssetUriFactory.CreateProjectAssetUri(fullPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public void Execute(LocalFile localFile)
}

var fullPath = localFile.LocalFilePath;
var fileName = Path.GetFileName(fullPath);

activeCartesianTileSubObjectColorLayer = Instantiate(layerGameObjectPrefab);
activeCartesianTileSubObjectColorLayer.gameObject.name = fileName;
activeCartesianTileSubObjectColorLayer.gameObject.name = localFile.OriginalFileName;
var propertyData = activeCartesianTileSubObjectColorLayer.PropertyData as CartesianTileSubObjectColorPropertyData;
propertyData.Data = AssetUriFactory.CreateProjectAssetUri(fullPath);

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
2 changes: 1 addition & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"eu.netherlands3d.masking": "2.0.1",
"eu.netherlands3d.meshclipper": "https://github.com/Netherlands3D/MeshClipper.git",
"eu.netherlands3d.minimap": "1.1.6",
"eu.netherlands3d.obj-importer": "1.1.0",
"eu.netherlands3d.obj-importer": "1.1.1",
"eu.netherlands3d.periodic-snapshots": "2.0.0",
"eu.netherlands3d.selection-tools": "2.6.0",
"eu.netherlands3d.simplejson": "1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
"url": "https://package.openupm.com"
},
"eu.netherlands3d.obj-importer": {
"version": "1.1.0",
"version": "1.1.1",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
Loading