Ver código fonte

Changes to images to store to file, return as URLs

master
GJWilliams87 4 anos atrás
pai
commit
adcc282303

+ 19
- 0
UnivateProperties_API/Containers/Property/ImageFormatter.cs Ver arquivo

@@ -60,5 +60,24 @@ namespace UnivateProperties_API.Containers.Property
60 60
             else
61 61
                 return "";
62 62
         }
63
+
64
+        public static string ImageToURL(string Path, string URL)
65
+        {
66
+            string url = URL;
67
+
68
+            string[] folders = Path.Split(new char[] { '\\' });
69
+            bool startAdd = false;
70
+            foreach(var folder in folders)
71
+            {
72
+                if (!startAdd && folder == "img")
73
+                    startAdd = true;
74
+
75
+                if (startAdd)
76
+                    url += folder + "/";
77
+            }
78
+
79
+
80
+            return url.Substring(0, url.Length - 1);
81
+        }
63 82
     }
64 83
 }

+ 24
- 20
UnivateProperties_API/Context/DataContext.cs Ver arquivo

@@ -1,5 +1,5 @@
1 1
 using Microsoft.EntityFrameworkCore;
2
-using Npgsql;
2
+using System.Data.SqlClient;
3 3
 using System.Linq;
4 4
 using UnivateProperties_API.Model;
5 5
 using UnivateProperties_API.Model.Banks;
@@ -27,6 +27,10 @@ namespace UnivateProperties_API.Context
27 27
                 {
28 28
                     connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString;
29 29
                 }
30
+                else if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension")
31
+                {
32
+                    connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString;
33
+                }
30 34
             }
31 35
         }
32 36
 
@@ -207,24 +211,24 @@ namespace UnivateProperties_API.Context
207 211
             modelBuilder.Entity<PlaceHolderFormat>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
208 212
         }
209 213
 
210
-    
211
-        //public int GetMaxID(string tableName)
212
-        //{
213
-        //    NpgsqlConnection connection = new NpgsqlConnection(connectionString);
214
-        //    connection.Open();
215
-
216
-        //    NpgsqlCommand cmd = connection.CreateCommand();
217
-        //    cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
218
-        //    NpgsqlDataReader reader = cmd.ExecuteReader();
219
-        //    int returnValue = 0; 
220
-
221
-        //    while(reader.Read())
222
-        //    {
223
-        //        returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
224
-        //    }
225
-
226
-        //    connection.Close();
227
-        //    return returnValue;
228
-        //}
214
+
215
+        public int GetMaxID(string tableName)
216
+        {
217
+            SqlConnection connection = new SqlConnection(connectionString);
218
+            connection.Open();
219
+
220
+            SqlCommand cmd = connection.CreateCommand();
221
+            cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
222
+            SqlDataReader reader = cmd.ExecuteReader();
223
+            int returnValue = 0;
224
+
225
+            while (reader.Read())
226
+            {
227
+                returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
228
+            }
229
+
230
+            connection.Close();
231
+            return returnValue;
232
+        }
229 233
     }
230 234
 }

+ 7
- 0
UnivateProperties_API/Controllers/Properties/PropertyImageController.cs Ver arquivo

@@ -41,6 +41,13 @@ namespace UnivateProperties_API.Controllers.Properties
41 41
             return new OkObjectResult(_Repo.Get(x => x.PropertyId == PropertyId));
42 42
         }
43 43
 
44
+        [HttpGet("SavePropImages")]
45
+        public IActionResult SavePropImages()
46
+        {
47
+            _Repo.UpdateToPhysical();
48
+            return new OkResult();
49
+        }
50
+
44 51
         [HttpPost]
45 52
         public IActionResult Post([FromBody] NewPropertyImages propertyImage)
46 53
         {

+ 1694
- 0
UnivateProperties_API/Migrations/20201110092829_URL added to location.Designer.cs
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo


+ 22
- 0
UnivateProperties_API/Migrations/20201110092829_URL added to location.cs Ver arquivo

@@ -0,0 +1,22 @@
1
+using Microsoft.EntityFrameworkCore.Migrations;
2
+
3
+namespace UnivateProperties_API.Migrations
4
+{
5
+    public partial class URLaddedtolocation : Migration
6
+    {
7
+        protected override void Up(MigrationBuilder migrationBuilder)
8
+        {
9
+            migrationBuilder.AddColumn<string>(
10
+                name: "SiteURL",
11
+                table: "Location",
12
+                nullable: true);
13
+        }
14
+
15
+        protected override void Down(MigrationBuilder migrationBuilder)
16
+        {
17
+            migrationBuilder.DropColumn(
18
+                name: "SiteURL",
19
+                table: "Location");
20
+        }
21
+    }
22
+}

+ 2
- 0
UnivateProperties_API/Migrations/DataContextModelSnapshot.cs Ver arquivo

@@ -556,6 +556,8 @@ namespace UnivateProperties_API.Migrations
556 556
 
557 557
                     b.Property<string>("PropertyImageLocation");
558 558
 
559
+                    b.Property<string>("SiteURL");
560
+
559 561
                     b.HasKey("Id");
560 562
 
561 563
                     b.ToTable("Location");

+ 1
- 0
UnivateProperties_API/Model/Misc/Location.cs Ver arquivo

@@ -4,5 +4,6 @@
4 4
     {
5 5
         public bool IsTesting { get; set; }
6 6
         public string PropertyImageLocation { get; set; }
7
+        public string SiteURL { get; set; }
7 8
     }
8 9
 }

+ 1
- 0
UnivateProperties_API/Repository/Properties/IPropertyImageRepository.cs Ver arquivo

@@ -8,5 +8,6 @@ namespace UnivateProperties_API.Repository.Properties
8 8
     {
9 9
         List<string> GetImages(int PropertyId);
10 10
         void Update(NewPropertyImages propertyImages);
11
+        void UpdateToPhysical();
11 12
     }
12 13
 }

+ 53
- 3
UnivateProperties_API/Repository/Properties/PropertyImageRepository.cs Ver arquivo

@@ -21,10 +21,17 @@ namespace UnivateProperties_API.Repository.Properties
21 21
         {
22 22
             var images = dBContext.PropertyImages.Where(where).OrderByDescending(i => i.IsDefault).ThenBy(i => i.Id).ToList();
23 23
 
24
+            var url = dBContext.Location.FirstOrDefault()?.SiteURL;
25
+
24 26
             foreach (PropertyImage img in images)
25 27
             {
26 28
                 if (!img.Image.StartsWith("data:image"))
27
-                    img.Image = ImageFormatter.ImageToBase64(img.Image);                
29
+                {
30
+                    if (!string.IsNullOrEmpty(url))
31
+                        img.Image = ImageFormatter.ImageToURL(img.Image, url);
32
+                    else
33
+                        img.Image = ImageFormatter.ImageToBase64(img.Image);
34
+                }
28 35
             }
29 36
 
30 37
             return images;
@@ -54,10 +61,17 @@ namespace UnivateProperties_API.Repository.Properties
54 61
 
55 62
             List<string> formated = new List<string>();
56 63
 
64
+            var url = dBContext.Location.FirstOrDefault()?.SiteURL;
65
+
57 66
             foreach (string img in images)
58 67
             {
59 68
                 if (!img.StartsWith("data:image"))
60
-                    formated.Add(ImageFormatter.ImageToBase64(img));
69
+                {
70
+                    if (!string.IsNullOrEmpty(url))
71
+                        formated.Add(ImageFormatter.ImageToURL(img, url));
72
+                    else
73
+                        formated.Add(ImageFormatter.ImageToBase64(img));
74
+                }
61 75
                 else
62 76
                     formated.Add(img);
63 77
             }
@@ -131,8 +145,12 @@ namespace UnivateProperties_API.Repository.Properties
131 145
                 if (!string.IsNullOrEmpty(loc))
132 146
                 {
133 147
                     saveFiles = true;
148
+                    if (loc.EndsWith("\\"))
149
+                    {
150
+                        loc = loc.Substring(0, loc.Length - 1);
151
+                    }
134 152
                     loc += string.Format("\\{0}", propertyImages.PropertyId);
135
-                    if (Directory.Exists(loc))
153
+                    if (!Directory.Exists(loc))
136 154
                     {
137 155
                         Directory.CreateDirectory(loc);
138 156
                     }
@@ -162,5 +180,37 @@ namespace UnivateProperties_API.Repository.Properties
162 180
                 Save();
163 181
             }
164 182
         }
183
+
184
+        public void UpdateToPhysical()
185
+        {            
186
+            bool saveFiles = false;
187
+            var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
188
+            if (!string.IsNullOrEmpty(loc))
189
+            {
190
+                saveFiles = true;
191
+                if (loc.EndsWith("\\"))
192
+                {
193
+                    loc = loc.Substring(0, loc.Length - 1);
194
+                }
195
+            }
196
+
197
+            if (saveFiles)
198
+            {
199
+                var images = dBContext.PropertyImages.ToList();
200
+                foreach (var image in images)
201
+                {
202
+                    string filePath = string.Format("{0}\\{1}", loc, image.PropertyId );
203
+                    if (!Directory.Exists(filePath))
204
+                    {
205
+                        Directory.CreateDirectory(filePath);
206
+                    }
207
+
208
+                    string path = ImageFormatter.Base64ToImage(image.Image, filePath, image.Id.ToString());
209
+                    image.Image = path;
210
+                }
211
+
212
+                Save();
213
+            }
214
+        }
165 215
     }
166 216
 }

+ 48
- 36
UnivateProperties_API/Repository/Properties/PropertyRepository.cs Ver arquivo

@@ -27,7 +27,7 @@ namespace UnivateProperties_API.Repository.Properties
27 27
         public List<Property> Get(Func<Property, bool> where)
28 28
         {
29 29
             return dBContext.Properties
30
-                .Include("PropertyType")                
30
+                .Include("PropertyType")
31 31
                 .Where(where).ToList();
32 32
         }
33 33
 
@@ -39,7 +39,7 @@ namespace UnivateProperties_API.Repository.Properties
39 39
 
40 40
         public Property GetDetailed(Func<Property, bool> first)
41 41
         {
42
-            throw new NotImplementedException();            
42
+            throw new NotImplementedException();
43 43
         }
44 44
 
45 45
         public PropertyContainer GetDetailed(int id, bool detailed)
@@ -72,10 +72,10 @@ namespace UnivateProperties_API.Repository.Properties
72 72
         }
73 73
 
74 74
         private PropertyContainer GetDetail(Property property, bool detailed)
75
-        {            
75
+        {
76 76
             int propID = property.Id;
77
-            var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);           
78
-            property.DisplayData = new List<PropertyDetailGroup>();            
77
+            var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
78
+            property.DisplayData = new List<PropertyDetailGroup>();
79 79
 
80 80
             if (detailed)
81 81
             {
@@ -236,7 +236,7 @@ namespace UnivateProperties_API.Repository.Properties
236 236
             {
237 237
                 if (prop != "Item" && prop != "Display")
238 238
                     property[prop] = item[prop];
239
-            }            
239
+            }
240 240
 
241 241
             property.PropertyUserFields = null;
242 242
             property.PropertyImages = null;
@@ -244,7 +244,7 @@ namespace UnivateProperties_API.Repository.Properties
244 244
             if (item.Price < item.OldPrice)
245 245
             {
246 246
                 property.PriceRedused = true;
247
-            }            
247
+            }
248 248
 
249 249
             if (!string.IsNullOrEmpty(item.StatusString))
250 250
             {
@@ -252,7 +252,7 @@ namespace UnivateProperties_API.Repository.Properties
252 252
                     property.StatusDate = DateTime.Now;
253 253
 
254 254
                 property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
255
-                if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)  
255
+                if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)
256 256
                 {
257 257
                     property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
258 258
                 }
@@ -300,7 +300,7 @@ namespace UnivateProperties_API.Repository.Properties
300 300
                             dBContext.Entry(propField).State = EntityState.Modified;
301 301
                             Save();
302 302
                         }
303
-                    }                    
303
+                    }
304 304
                 }
305 305
             }
306 306
             #endregion
@@ -329,23 +329,30 @@ namespace UnivateProperties_API.Repository.Properties
329 329
             }
330 330
 
331 331
             if (item.NewImages != null)
332
-            {                
332
+            {
333 333
                 bool saveFiles = false;
334 334
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
335 335
                 if (!string.IsNullOrEmpty(loc))
336 336
                 {
337 337
                     saveFiles = true;
338
+                    if (loc.EndsWith("\\"))
339
+                    {
340
+                        loc = loc.Substring(0, loc.Length - 1);
341
+                    }
338 342
                     loc += string.Format("\\{0}", property.Id);
339
-                    if (Directory.Exists(loc))
343
+                    if (!Directory.Exists(loc))
340 344
                     {
341 345
                         Directory.CreateDirectory(loc);
342 346
                     }
343 347
                 }
344 348
 
345
-                             
349
+
346 350
                 var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
347 351
                 foreach (var image in item.NewImages)
348 352
                 {
353
+                    if (!image.Image.StartsWith("data:image")) 
354
+                        continue;
355
+
349 356
                     var propImage = new PropertyImage
350 357
                     {
351 358
                         PropertyId = property.Id,
@@ -366,7 +373,7 @@ namespace UnivateProperties_API.Repository.Properties
366 373
                         dBContext.PropertyImages.Add(propImage);
367 374
                         Save();
368 375
                         lastID++;
369
-                    }                                        
376
+                    }
370 377
                 }
371 378
             }
372 379
 
@@ -391,7 +398,7 @@ namespace UnivateProperties_API.Repository.Properties
391 398
 
392 399
             return GetDisplayDetails(props);
393 400
         }
394
-        
401
+
395 402
         public List<PropertyDisplay> GetDisplay(PropertySearch search)
396 403
         {
397 404
             //return GetDisplayDetails(dBContext.Properties.ToList());
@@ -410,7 +417,7 @@ namespace UnivateProperties_API.Repository.Properties
410 417
             {
411 418
                 string keyword = search.Keyword.ToLower();
412 419
 
413
-                List<Property> props = (from p in dBContext.Properties                                       
420
+                List<Property> props = (from p in dBContext.Properties
414 421
                                         join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
415 422
                                         where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
416 423
                                         || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
@@ -474,7 +481,7 @@ namespace UnivateProperties_API.Repository.Properties
474 481
 
475 482
                 if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
476 483
                 {
477
-                    props = (from p in props                            
484
+                    props = (from p in props
478 485
                              where p.Province.ToUpper() == search.Province.ToUpper()
479 486
                              select p).ToList();
480 487
 
@@ -485,7 +492,7 @@ namespace UnivateProperties_API.Repository.Properties
485 492
 
486 493
                 if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
487 494
                 {
488
-                    props = (from p in props                             
495
+                    props = (from p in props
489 496
                              where p.City.ToUpper() == search.City.ToUpper()
490 497
                              select p).ToList();
491 498
 
@@ -496,7 +503,7 @@ namespace UnivateProperties_API.Repository.Properties
496 503
 
497 504
                 if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
498 505
                 {
499
-                    props = (from p in props                             
506
+                    props = (from p in props
500 507
                              where p.Suburb.ToUpper() == search.Suburb.ToUpper()
501 508
                              select p).ToList();
502 509
 
@@ -544,7 +551,7 @@ namespace UnivateProperties_API.Repository.Properties
544 551
 
545 552
                 return GetDisplayDetails(props);
546 553
             }
547
-        }        
554
+        }
548 555
 
549 556
         private void SaveLog(SearchObject item)
550 557
         {
@@ -552,7 +559,7 @@ namespace UnivateProperties_API.Repository.Properties
552 559
             {
553 560
                 Type = item.Type,
554 561
                 Search = JsonConvert.SerializeObject(item)
555
-            };            
562
+            };
556 563
             dBContext.SearchLogs.Add(searchLog);
557 564
             Save();
558 565
         }
@@ -566,7 +573,7 @@ namespace UnivateProperties_API.Repository.Properties
566 573
                 PropertyDisplay display = new PropertyDisplay
567 574
                 {
568 575
                     PropertyReference = item.PropertyRef,
569
-                    DateAvailable = item.DateAvailable,                    
576
+                    DateAvailable = item.DateAvailable,
570 577
                     Id = item.Id,
571 578
                     ShortDescription = item.ShortDescription,
572 579
                     IsSale = item.IsSale,
@@ -686,8 +693,8 @@ namespace UnivateProperties_API.Repository.Properties
686 693
                     break;
687 694
             }
688 695
 
689
-            List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published 
690
-                        && x.PropertyType.UsageType == type 
696
+            List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published
697
+                        && x.PropertyType.UsageType == type
691 698
                         && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
692 699
             return GetDisplayDetails(props);
693 700
         }
@@ -724,7 +731,7 @@ namespace UnivateProperties_API.Repository.Properties
724 731
         }
725 732
 
726 733
         public List<PropertyList> GetPropertyList()
727
-        {           
734
+        {
728 735
             return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
729 736
         }
730 737
 
@@ -774,7 +781,7 @@ namespace UnivateProperties_API.Repository.Properties
774 781
 
775 782
         public void Insert(PropertyContainer items)
776 783
         {
777
-            Property property = new Property();            
784
+            Property property = new Property();
778 785
             PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
779 786
             if (pt != null)
780 787
             {
@@ -809,9 +816,9 @@ namespace UnivateProperties_API.Repository.Properties
809 816
             items.PropertyUserFields = null;
810 817
 
811 818
             var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
812
-            var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();            
819
+            var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
813 820
 
814
-            foreach( string prop in property.GetAllProperties())
821
+            foreach (string prop in property.GetAllProperties())
815 822
             {
816 823
                 if (prop != "Item" && prop != "Display")
817 824
                     property[prop] = items[prop];
@@ -823,9 +830,9 @@ namespace UnivateProperties_API.Repository.Properties
823 830
             {
824 831
                 property.AgencyId = agent.AgencyId;
825 832
                 property.AgentId = agent.Id;
826
-            }            
833
+            }
827 834
 
828
-            if (property.IsSale)            
835
+            if (property.IsSale)
829 836
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
830 837
             else
831 838
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
@@ -836,12 +843,17 @@ namespace UnivateProperties_API.Repository.Properties
836 843
             if (images != null)
837 844
             {
838 845
                 bool saveFiles = false;
846
+                var nextPropID = dBContext.GetMaxID("Properties") + 1;
839 847
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
840 848
                 if (!string.IsNullOrEmpty(loc))
841 849
                 {
842 850
                     saveFiles = true;
843
-                    loc += string.Format("\\{0}", property.Id);
844
-                    if (Directory.Exists(loc))
851
+                    if (loc.EndsWith("\\"))
852
+                    {
853
+                        loc = loc.Substring(0, loc.Length - 1);
854
+                    }
855
+                    loc += string.Format("\\{0}", nextPropID);
856
+                    if (!Directory.Exists(loc))
845 857
                     {
846 858
                         Directory.CreateDirectory(loc);
847 859
                     }
@@ -851,7 +863,7 @@ namespace UnivateProperties_API.Repository.Properties
851 863
                 var lastID = 0;
852 864
                 if (dBContext.PropertyImages.Count() == 0)
853 865
                     lastID = 1;
854
-                else 
866
+                else
855 867
                     lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
856 868
 
857 869
                 foreach (PropertyImage image in images)
@@ -868,7 +880,7 @@ namespace UnivateProperties_API.Repository.Properties
868 880
             }
869 881
 
870 882
             if (fields != null)
871
-            {                
883
+            {
872 884
                 property.PropertyUserFields = new List<PropertyUserField>();
873 885
                 foreach (PropertyUserField field in fields)
874 886
                 {
@@ -880,7 +892,7 @@ namespace UnivateProperties_API.Repository.Properties
880 892
             dBContext.Properties.Add(property);
881 893
             Save();
882 894
 
883
-            items.Id = property.Id;            
895
+            items.Id = property.Id;
884 896
         }
885 897
 
886 898
         public bool MayEdit(int id)
@@ -912,7 +924,7 @@ namespace UnivateProperties_API.Repository.Properties
912 924
                 }
913 925
 
914 926
                 foreach (PropertyImage image in Images)
915
-                {                                    
927
+                {
916 928
                     image.PropertyId = propertyID;
917 929
                     if (saveFiles)
918 930
                     {
@@ -994,7 +1006,7 @@ namespace UnivateProperties_API.Repository.Properties
994 1006
                     IsPublished = prop.Published,
995 1007
                     Type = prop.PropertyType.UsageType.ToString()
996 1008
                 };
997
-                
1009
+
998 1010
                 if (prop.StatusId != null)
999 1011
                 {
1000 1012
                     propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;

Carregando…
Cancelar
Salvar