Skip to content

Commit abfbe58

Browse files
author
bhuvaneswari
committed
feature(EJ2-38936): Added the upload customization support for firebase file provider
1 parent bad3f64 commit abfbe58

File tree

3 files changed

+165
-32
lines changed

3 files changed

+165
-32
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Firebase realtime database file provider
6+
7+
#### New Features
8+
9+
- Support has been provided for upload customization.
10+
311
## 17.3.0.14 (2019-10-04)
412

5-
### Firebase realtime database file system provider
13+
### Firebase realtime database file provider
614

715
#### New Features
816

9-
- Added Firebase realtime cloud storage database filesystem provider support for ASP.NET Core.
17+
- Added Firebase realtime cloud storage database file provider support for ASP.NET Core.

Models/FirebaseRealtimeDBFileProvider.cs

Lines changed: 154 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -732,40 +732,165 @@ public FileStreamResult GetImage(string path, string id, bool allowCompress, Ima
732732
public virtual FileManagerResponse Upload(string path, IList<IFormFile> uploadFiles, string action, params FileManagerDirectoryContent[] data)
733733
{
734734
FileManagerResponse uploadResponse = new FileManagerResponse();
735+
List<string> existFiles = new List<string>();
735736
try
736737
{
737-
string filename = Path.GetFileName(uploadFiles[0].FileName);
738-
string contentType = uploadFiles[0].ContentType;
739-
int idValue = this.firebaseGetData.Select(x => x.Id).ToArray().Select(int.Parse).ToArray().Max();
740-
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Create))
738+
foreach (IFormFile file in uploadFiles)
741739
{
742-
uploadFiles[0].CopyTo(fsSource);
743-
fsSource.Close();
740+
string name = file.FileName;
741+
string fullName = Path.Combine(Path.GetTempPath(), name);
742+
if (uploadFiles != null)
743+
{
744+
string filename = Path.GetFileName(uploadFiles[0].FileName);
745+
string contentType = uploadFiles[0].ContentType;
746+
int idValue = this.firebaseGetData.Select(x => x.Id).ToArray().Select(int.Parse).ToArray().Max();
747+
string[] fileNames = this.firebaseGetData.Select(x => x.Name).ToArray();
748+
if (action == "save")
749+
{
750+
if (!System.IO.File.Exists(fullName))
751+
{
752+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Create))
753+
{
754+
uploadFiles[0].CopyTo(fsSource);
755+
fsSource.Close();
756+
}
757+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Open, FileAccess.Read))
758+
{
759+
BinaryReader br = new BinaryReader(fsSource);
760+
long numBytes = new FileInfo(Path.Combine(Path.GetTempPath(), filename)).Length;
761+
byte[] bytes = br.ReadBytes((int)numBytes);
762+
this.GetRelativePath(data[0].Id, "/");
763+
this.GetRelativeId(data[0].Id);
764+
FileManagerDirectoryContent CreateData = new FileManagerDirectoryContent()
765+
{
766+
Id = (idValue + 1).ToString(),
767+
Name = filename,
768+
Size = bytes.Length,
769+
DateCreated = DateTime.Now.ToString(),
770+
DateModified = DateTime.Now.ToString(),
771+
Type = System.IO.Path.GetExtension(filename),
772+
HasChild = false,
773+
ParentId = data[0].Id,
774+
IsFile = true,
775+
Content = bytes,
776+
isRoot = (data[0].Id.ToString() == "0") ? true : false,
777+
FilterPath = this.filterPath.Substring(this.rootNode.Length) + "/",
778+
FilterId = this.filterId + "/"
779+
};
780+
this.updateDBNode(CreateData, idValue);
781+
}
782+
}
783+
else
784+
{
785+
existFiles.Add(name);
786+
}
787+
}
788+
else if (action == "replace")
789+
{
790+
int i = 0;
791+
FileManagerDirectoryContent[] childs = this.firebaseGetData.Select(x => x).ToArray();
792+
foreach (string newFile in fileNames)
793+
{
794+
if (newFile == filename)
795+
{
796+
while (i < fileNames.Length)
797+
{
798+
if (filename == fileNames[i])
799+
{
800+
int id = i;
801+
this.DeleteItems(childs[id].Id);
802+
}
803+
i++;
804+
}
805+
}
806+
}
807+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Create))
808+
{
809+
file.CopyTo(fsSource);
810+
fsSource.Close();
811+
}
812+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Open, FileAccess.Read))
813+
{
814+
BinaryReader br = new BinaryReader(fsSource);
815+
long numBytes = new FileInfo(Path.Combine(Path.GetTempPath(), filename)).Length;
816+
byte[] bytes = br.ReadBytes((int)numBytes);
817+
this.GetRelativePath(data[0].Id, "/");
818+
this.GetRelativeId(data[0].Id);
819+
FileManagerDirectoryContent CreateData = new FileManagerDirectoryContent()
820+
{
821+
Id = (idValue + 1).ToString(),
822+
Name = filename,
823+
Size = bytes.Length,
824+
DateCreated = DateTime.Now.ToString(),
825+
DateModified = DateTime.Now.ToString(),
826+
Type = System.IO.Path.GetExtension(filename),
827+
HasChild = false,
828+
ParentId = data[0].Id,
829+
IsFile = true,
830+
Content = bytes,
831+
isRoot = (data[0].Id.ToString() == "0") ? true : false,
832+
FilterPath = this.filterPath.Substring(this.rootNode.Length) + "/",
833+
FilterId = this.filterId + "/"
834+
};
835+
this.updateDBNode(CreateData, idValue);
836+
}
837+
}
838+
else if (action == "keepboth")
839+
{
840+
string newName = fullName;
841+
string newFileName = file.FileName;
842+
int index = fullName.LastIndexOf(".");
843+
int indexValue = newFileName.LastIndexOf(".");
844+
if (index >= 0)
845+
{
846+
newName = fullName.Substring(0, index);
847+
newFileName = newFileName.Substring(0, indexValue);
848+
}
849+
int fileCount = 0;
850+
while (System.IO.File.Exists(newName + (fileCount > 0 ? "(" + fileCount.ToString() + ")" + Path.GetExtension(name) : Path.GetExtension(name))))
851+
{
852+
fileCount++;
853+
}
854+
newName = newFileName + (fileCount > 0 ? "(" + fileCount.ToString() + ")" : "") + Path.GetExtension(name);
855+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), newName), FileMode.Create))
856+
{
857+
file.CopyTo(fsSource);
858+
fsSource.Close();
859+
}
860+
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), newName), FileMode.Open, FileAccess.Read))
861+
{
862+
BinaryReader br = new BinaryReader(fsSource);
863+
long numBytes = new FileInfo(Path.Combine(Path.GetTempPath(), newName)).Length;
864+
byte[] bytes = br.ReadBytes((int)numBytes);
865+
this.GetRelativePath(data[0].Id, "/");
866+
this.GetRelativeId(data[0].Id);
867+
FileManagerDirectoryContent CreateData = new FileManagerDirectoryContent()
868+
{
869+
Id = (idValue + 1).ToString(),
870+
Name = newName,
871+
Size = bytes.Length,
872+
DateCreated = DateTime.Now.ToString(),
873+
DateModified = DateTime.Now.ToString(),
874+
Type = System.IO.Path.GetExtension(newName),
875+
HasChild = false,
876+
ParentId = data[0].Id,
877+
IsFile = true,
878+
Content = bytes,
879+
isRoot = (data[0].Id.ToString() == "0") ? true : false,
880+
FilterPath = this.filterPath.Substring(this.rootNode.Length) + "/",
881+
FilterId = this.filterId + "/"
882+
};
883+
this.updateDBNode(CreateData, idValue);
884+
}
885+
}
886+
}
744887
}
745-
using (FileStream fsSource = new FileStream(Path.Combine(Path.GetTempPath(), filename), FileMode.Open, FileAccess.Read))
888+
if (existFiles.Count != 0)
746889
{
747-
BinaryReader br = new BinaryReader(fsSource);
748-
long numBytes = new FileInfo(Path.Combine(Path.GetTempPath(), filename)).Length;
749-
byte[] bytes = br.ReadBytes((int)numBytes);
750-
this.GetRelativePath(data[0].Id, "/");
751-
this.GetRelativeId(data[0].Id);
752-
FileManagerDirectoryContent CreateData = new FileManagerDirectoryContent()
753-
{
754-
Id = (idValue + 1).ToString(),
755-
Name = filename,
756-
Size = bytes.Length,
757-
DateCreated = DateTime.Now.ToString(),
758-
DateModified = DateTime.Now.ToString(),
759-
Type = System.IO.Path.GetExtension(filename),
760-
HasChild = false,
761-
ParentId = data[0].Id,
762-
IsFile = true,
763-
Content = bytes,
764-
isRoot = (data[0].Id.ToString() == "0") ? true : false,
765-
FilterPath = this.filterPath.Substring(this.rootNode.Length) + "/",
766-
FilterId = this.filterId + "/"
767-
};
768-
this.updateDBNode(CreateData, idValue);
890+
ErrorDetails er = new ErrorDetails();
891+
er.Code = "400";
892+
er.Message = "File Already Exists";
893+
uploadResponse.Error = er;
769894
}
770895
}
771896
catch (Exception e)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ Check the license detail [here](https://github.com/syncfusion/ej2-javascript-ui-
118118

119119
Check the changelog [here](https://github.com/syncfusion/ej2-javascript-ui-controls/blob/master/controls/filemanager/CHANGELOG.md)
120120

121-
© Copyright 2019 Syncfusion, Inc. All Rights Reserved. The Syncfusion Essential Studio license and copyright applies to this distribution.
121+
© Copyright 2020 Syncfusion, Inc. All Rights Reserved. The Syncfusion Essential Studio license and copyright applies to this distribution.

0 commit comments

Comments
 (0)