public user_share_file addFiles() { var newFile = new file() { name = "test222", type = 1, url = "test222", }; using (FileEntitiesFinal entities = new FileEntitiesFinal()) { entities.Entry <file>(newFile).State = System.Data.Entity.EntityState.Added; entities.SaveChanges(); entities.Entry(newFile); user_share_file newShare = new user_share_file() { user_id = 2, file_id = newFile.id, code = "1234" }; entities.user_share_file.Add(newShare); entities.SaveChanges(); return(newShare); } }
public HttpResponseMessage deleteFile(string id) { //string id = HttpContext.Current.Request.Params["fileId"].ToString(); int fileId = 0; HttpResponseMessage response = null; //response.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue() //{ // MaxAge = TimeSpan.FromMinutes(20) //}; try { fileId = Convert.ToInt32(id); } catch (Exception e) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid File"); return(response); } var userId = HttpContext.Current.Session["id"]; using (FileEntitiesFinal entity = new FileEntitiesFinal()) { var files = from f in entity.file where f.id == fileId select f; file tempFile = files.FirstOrDefault(); if (tempFile.user_id != (int)userId) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Operation deny")); } if (tempFile.status == 100) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "File had been deleted, don't delete it again."); return(response); } tempFile.status = 100; entity.Entry <file>(tempFile).State = System.Data.Entity.EntityState.Modified; entity.SaveChanges(); string message = string.Format("Delete file {0} successfully!", tempFile.name.ToString()); //response.Content = new StringContent(message, Encoding.Unicode); response = Request.CreateResponse(HttpStatusCode.OK, message); return(response); } }
private async void addUpFileNumbs(int userId) { using (TransactionScope scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { user tempUser = entity.user.Where(u => u.id == userId).FirstOrDefault(); if (tempUser == null) { return; } tempUser.file_nums++; entity.user.Attach(tempUser); entity.Entry(tempUser).State = System.Data.Entity.EntityState.Modified; await entity.SaveChangesAsync(); } } }
//private string GetRandomString(int length) //{ // const string key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // if (length < 1) // { // return string.Empty; // } // Random random = new Random(); // byte[] buffer = new byte[8]; // ulong bit = 21; // ulong result = 0; // int index = 0; // StringBuilder stringBuilder = new StringBuilder((length / 5 + 1) * 5); // while (stringBuilder.Length <length) // { // random.NextBytes(buffer); // buffer[5] = buffer[6] = buffer[7] = 0x00; // result = BitConverter.ToUInt64(buffer, 0); // while (result >0 && stringBuilder.Length <length) // { // index = (int)(bit & result); // stringBuilder.Append(key[index]); // result = result >> 5; // } // } // return stringBuilder.ToString(); // } private async void addUpDownloadTimes(int fileId) { using (TransactionScope scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { file tempFile = entity.file.Where(f => f.id == fileId).FirstOrDefault(); if (tempFile == null) { return; } else { tempFile.download_times++; entity.file.Attach(tempFile); entity.Entry(tempFile).State = System.Data.Entity.EntityState.Modified; await entity.SaveChangesAsync(); } } scope.Complete(); } }