//Upload a new content object. Returns the pid of the uploaded file public string UploadFile(byte[] data, string pid, string key) { if (!CheckKey(key)) return null; //Check permissions if (!DoValidate(Security.TransactionType.Create, null)) return "Not authorized"; vwarDAL.ContentObject co = null; //Create a new object if (pid == "") { co = GetRepo().GetNewContentObject(); co.Revision = 0; //Setup some default values co.Title = "tempupload"; co.Views = 0; } if (pid != "") { co = GetRepo().GetContentObjectById(pid, false); if (co == null) { ReleaseRepo(); return "PID does not exist"; } co.Revision = co.Revision + 1; } co.UploadedDate = DateTime.Now; co.LastModified = DateTime.Now; //The owner of this content is the person whose credentials were used to upload it co.SubmitterEmail = GetUserEmail(); Utility_3D.ConvertedModel model; try { //Setup the conversion library Utility_3D _3d = new Utility_3D(); _3d.Initialize(ConfigurationManager.AppSettings["LibraryLocation"]); Utility_3D.Model_Packager converter = new Utility_3D.Model_Packager(); Utility_3D.ConverterOptions opts = new Utility_3D.ConverterOptions(); //We do want metadata gathered with this conversion opts.EnableMetadataGathering(); opts.EnableScaleTextures(512); opts.EnableTextureConversion("png"); //Try to convert the model package into a dae //Note that the system might allow you to input an skp, so this should probably take a filename //The conversion needs to be told that the input is skp, and currently it's hardcoded to show it as a zip model = converter.Convert(new MemoryStream(data), "content.zip", "dae", opts); } catch (Utility_3D.ConversionException e) { model = null; } catch (System.Exception e) { model = null; } if (model != null) { //Copy the data gathered by the converter to the metadata co.NumPolygons = model._ModelData.VertexCount.Polys; co.NumTextures = model.textureFiles.Count; co.UpAxis = model._ModelData.TransformProperties.UpAxis; co.UnitScale = model._ModelData.TransformProperties.UnitMeters.ToString(); co.LastModified = System.DateTime.Now; co.Views = 0; } co.UploadedDate = System.DateTime.Now; //Place this new object in the repo if (pid != "") { GetRepo().InsertContentRevision(co); } else { GetRepo().InsertContentObject(co); } if (model != null) { //Set the stream from the conversion to the content of this object co.SetContentFile(new MemoryStream(model.data), "content.zip"); //Set the display file Stream displayfile = ConvertFileToO3D(new MemoryStream(model.data)); if (displayfile != null) co.SetDisplayFile(displayfile, "content.o3d"); //Add the references to textrues discovered by the converter to the database foreach (string i in model._ModelData.ReferencedTextures) co.AddTextureReference(i.ToLower(), "Diffuse", 0); //Add the references to missing textures to the database foreach (string i in model.missingTextures) co.AddMissingTexture(i.ToLower(), "Diffuse", 0); } //set the original file data co.OriginalFileName = "OriginalUpload.zip"; co.OriginalFileId = GetRepo().SetContentFile(new MemoryStream(data), co.PID, co.OriginalFileName); co.CommitChanges(); //setup the default permissions vwarDAL.PermissionsManager perm = new vwarDAL.PermissionsManager(); perm.SetModelToGroupLevel(GetUserEmail(), co.PID, vwarDAL.DefaultGroups.AllUsers, vwarDAL.ModelPermissionLevel.Fetchable); perm.SetModelToGroupLevel(GetUserEmail(), co.PID, vwarDAL.DefaultGroups.AnonymousUsers, vwarDAL.ModelPermissionLevel.Searchable); perm.Dispose(); ReleaseRepo(); //return the pid of this new object return co.PID; }
//Upload the screenshot for the model public string SetGroupPermission(string pid, string groupname, string level, string key) { if (!CheckKey(key)) return null; pid = pid.Replace('_', ':'); //Get the content obhect vwarDAL.ContentObject co = GetRepo().GetContentObjectById(pid, false); //Check the permissions if (!DoValidate(Security.TransactionType.Modify, pid)) { ReleaseRepo(); return ""; } vwarDAL.PermissionsManager perm = new vwarDAL.PermissionsManager(); vwarDAL.PermissionErrorCode code = perm.SetModelToGroupLevel(GetUsername(), pid, groupname, (vwarDAL.ModelPermissionLevel)Enum.Parse(typeof(vwarDAL.ModelPermissionLevel), level)); ReleaseRepo(); return System.Enum.GetName(typeof(vwarDAL.PermissionErrorCode), code); }