-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
142 lines (126 loc) · 3.02 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require "sinatra"
require 'securerandom'
UUID = SecureRandom
def video_tag_load
json = ""
File.open(path="./video_tag.json", mode="r") do |file|
file.each do |text|
json += text
end
end
JSON.parse json
end
get "/" do
send_file "./web/index.html"
end
get "/post" do
send_file "./web/post.html"
end
post "/video" do
title = params[:title]
text = params[:text]
video_byte = params[:video][:tempfile].read
img_byte = params[:img][:tempfile].read
puts "title: #{title}"
video_titles = video_tag_load
video_token = UUID.uuid_v4
Save_file_name = "./videos/#{video_token}.mp4"
Save_img_path = "./img/#{video_token}.img"
video_titles[video_token] = {
"title" => title,
"text" => text,
"img" => Save_img_path,
"views" => 0,
"comment" => {},
"good" => 0,
"bad" => 0
}
#Seve video info
File.open(path="./video_tag.json", mode="w") do |file|
file.puts JSON.generate video_titles
end
#Write video
File.open(path=Save_file_name, mode="wb") do |file|
file.write video_byte
end
#Write img
File.open(path=Save_img_path, mode="wb") do |file|
file.write img_byte
end
video_token
end
get "/watch" do
send_file "./web/watch.html"
end
get "/video_watch" do
video_id = params[:v]
send_file "./videos/#{video_id}.mp4"
end
get "/getTitle" do
video_id = params[:id]
puts "video_id: #{video_id}"
json = ""
File.open(path="./video_tag.json", mode="r") do |file|
file.each do |text|
json += text
end
end
json = JSON.parse json
json[video_id]["views"] += 1
File.open(path="./video_tag.json", mode="w") do |file|
file.write JSON.generate json
end
puts json[video_id]
JSON.generate json[video_id]
end
post "/video_value" do
data = params
video_id = data[:video_id]
video_value = data[:value]
puts "video_id: #{video_id}.\nvalue: #{video_value}"
video_data = ""
File.open(path="./video_tag.json", mode="r") do |file|
file.each do |text|
video_data += text
end
end
video_data = JSON.parse video_data
puts "video_data: #{video_data}"
video_data[video_id][video_value] += 1
File.open(path="./video_tag.json", mode="w") do |file|
file.write JSON.generate(video_data)
end
JSON.generate video_data
end
post "/comments" do
puts params
video_id = params[:video_id]
comment_text = params[:comment]
user_name = params[:user]
puts "video_id: #{video_id}\nuser: #{user_name}\ncomment: #{comment_text}"
video_data = ""
File.open(path="./video_tag.json", mode="r") do |file|
file.each do |text|
video_data += text
end
end
video_data = JSON.parse video_data
video_data[video_id]["comment"][comment_text] = user_name
File.open(path="./video_tag.json", mode="w") do |file|
file.write JSON.generate(video_data)
end
JSON.generate(video_data)
end
get "/img" do
img_id = params[:img_id]
send_file "./img/#{img_id}.img"
end
get "/img_list" do
img_list = ""
File.open(path="./video_tag.json", mode="r") do |file|
file.each do |text|
img_list += text
end
end
img_list
end