Advertisement
Guest User

Untitled

a guest
May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <template>
  2. <div >
  3. <p v-if="errors">
  4. {{errorMessage}}
  5. </p>
  6. <label class="uploader__label">
  7. {{this.title}}
  8. </label>
  9. <!-- <img class="uploader__img" v-if="url" :src="url" /> -->
  10. <button
  11. :loading="uploading"
  12. :disabled="uploading"
  13. class="button button--orange"
  14. @click.prevent="$refs.file.click()"
  15. :class="success ? 'finished': ''"
  16. >
  17.  
  18.  
  19. <div v-if="uploading" class="lds-ring"><div></div><div></div><div></div><div></div></div>
  20.  
  21.  
  22. {{ success ? "Upload Successful " : "Upload a Video" }}
  23. </button>
  24. <input type="file" id="file" ref="file" v-on:change="onFileChange()" hidden/>
  25. </div>
  26. </template>
  27. <script>
  28. var parseString = require('xml2js').parseString
  29. import axios from 'axios'
  30. var instance = axios.create()
  31. // delete instance.defaults.headers.common.Authorization
  32.  
  33. export default {
  34. props: ['files', 'title', 'value'],
  35. data () {
  36. return {
  37. file: '',
  38. path: null,
  39. policy: null,
  40. direct_url: null,
  41. success: false,
  42. errors: false,
  43. uploading: false,
  44. errorMessage: null
  45. }
  46. },
  47. watch: {
  48. errors() {
  49. this.uploading = false
  50. },
  51. success() {
  52. this.uploading = false
  53. },
  54. },
  55. computed: {
  56. url() {
  57. if(this.file && this.success) {
  58. return URL.createObjectURL(this.file)
  59. } else if(this.value) {
  60. return this.value
  61. }
  62. return null
  63. }
  64. },
  65. methods: {
  66. setFormData() {
  67. let policy = this.policy
  68. if(policy) {
  69. var formData = new FormData()
  70. formData.append('key', policy.key)
  71. formData.append('Content-Type', this.file.type)
  72. formData.append('success_action_status', policy.success_action_status)
  73. formData.append('policy', policy.policy)
  74. formData.append('acl', policy.acl)
  75. formData.append('x-amz-credential', policy['x-amz-credential'])
  76. formData.append('x-amz-algorithm', policy['x-amz-algorithm'])
  77. formData.append('x-amz-date', policy['x-amz-date'])
  78. formData.append('x-amz-signature', policy['x-amz-signature'])
  79. formData.append('file', this.file)
  80. return formData
  81. } else {
  82. throw 'No Policy found. Make sure you have generated a secure URL'
  83. }
  84. },
  85. markSuccess() {
  86. this.success = true
  87. this.errors = false
  88. //this.value = this.path
  89. this.$emit('input', this.path)
  90. },
  91. async saveFileTos3(formData) {
  92. try {
  93. let awsResponse = await fetch(this.direct_url, {
  94. method: 'post',
  95. body: formData,
  96. })
  97. //let awsResponse = await instance.post(this.direct_url, formData)
  98.  
  99. let xml = await awsResponse.text()
  100.  
  101. parseString(xml, (err, result) => {
  102. console.log(result)
  103. if(!err) {
  104. this.path = result.PostResponse.Key[0]
  105. this.markSuccess()
  106. }
  107. })
  108. } catch(err) {
  109. throw "Upload Failed. Make sure the file is a valid video file (.mp4, .mov, .avi)"
  110. }
  111. },
  112. async getPolicy() {
  113. try {
  114. let signedUrl = await axios.post('/api/public/upload-url', {
  115. name: this.file.name,
  116. content_type: this.file.type
  117. })
  118. this.policy = signedUrl.data.data.url_fields
  119. this.direct_url = signedUrl.data.data.direct_url
  120. } catch(err) {
  121. throw "Api failed to generate a valid upload URL. Please Verify that proper ENV files are configued"
  122. }
  123. },
  124. async onFileChange(){
  125. try {
  126. this.uploading = true
  127. this.file = this.$refs.file.files[0]
  128. if (this.file) {
  129. await this.getPolicy()
  130. let formData = this.setFormData()
  131. await this.saveFileTos3(formData)
  132. } else {
  133. throw 'No file'
  134. }
  135. } catch(err) {
  136. this.errors = true
  137. this.errorMessage = err
  138. console.log(err)
  139. }
  140. }
  141. },
  142. }
  143. </script>
  144. <style lang="scss" >
  145. .button {
  146. margin: 0 auto;
  147. position: relative;
  148. }
  149.  
  150. .button.finished {
  151. background: $shred-black;
  152. border: 3px solid $shred-black;
  153. color: $shred-white;
  154. }
  155. .uploader__img{
  156. display: block;
  157. max-width: 135px;
  158. padding-left: 10px;
  159. }
  160. .uploader__label {
  161. padding-left: 10px;
  162. display: block;
  163. }
  164.  
  165. .lds-ring {
  166. display: inline-block;
  167. position: absolute;
  168. width: 21px;
  169. height: 21px;
  170. top: 45%;
  171. transform: translateY(-50%);
  172. left: 5px;
  173. }
  174. .lds-ring div {
  175. box-sizing: border-box;
  176. display: block;
  177. position: absolute;
  178. width: 21px;
  179. height: 21px;
  180. margin: 2px;
  181. border: 2px solid #fff;
  182. border-radius: 50%;
  183. animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
  184. border-color: #fff transparent transparent transparent;
  185. }
  186. .lds-ring div:nth-child(1) {
  187. animation-delay: -0.45s;
  188. }
  189. .lds-ring div:nth-child(2) {
  190. animation-delay: -0.3s;
  191. }
  192. .lds-ring div:nth-child(3) {
  193. animation-delay: -0.15s;
  194. }
  195. @keyframes lds-ring {
  196. 0% {
  197. transform: rotate(0deg);
  198. }
  199. 100% {
  200. transform: rotate(360deg);
  201. }
  202. }
  203.  
  204. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement