uploadable file
This commit is contained in:
		@@ -45,6 +45,7 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Map from global var to bool specifying if it can be redefined
 | 
					    // Map from global var to bool specifying if it can be redefined
 | 
				
			||||||
    globals: {
 | 
					    globals: {
 | 
				
			||||||
 | 
					        File: true,
 | 
				
			||||||
        jest: true,
 | 
					        jest: true,
 | 
				
			||||||
        __DEV__: true,
 | 
					        __DEV__: true,
 | 
				
			||||||
        __dirname: false,
 | 
					        __dirname: false,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,4 +6,5 @@ func AddHandlers() {
 | 
				
			|||||||
	addTagHandlers()
 | 
						addTagHandlers()
 | 
				
			||||||
	addActorsHandlers()
 | 
						addActorsHandlers()
 | 
				
			||||||
	addTvshowHandlers()
 | 
						addTvshowHandlers()
 | 
				
			||||||
 | 
						addUploadHandler()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										60
									
								
								apiGo/api/FileUpload.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								apiGo/api/FileUpload.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
				
			|||||||
 | 
					package api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"openmediacenter/apiGo/api/api"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func addUploadHandler() {
 | 
				
			||||||
 | 
						api.AddHandler("fileupload", api.VideoNode, api.PermUser, func(ctx api.Context) {
 | 
				
			||||||
 | 
							fmt.Println("we are in file upload handler")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							req := ctx.GetRequest()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							mr, err := req.MultipartReader()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							length := req.ContentLength
 | 
				
			||||||
 | 
							fmt.Println(length)
 | 
				
			||||||
 | 
							for {
 | 
				
			||||||
 | 
								part, err := mr.NextPart()
 | 
				
			||||||
 | 
								if err == io.EOF {
 | 
				
			||||||
 | 
									fmt.Println("part is eof")
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								fmt.Println(part.FormName())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								var read int64
 | 
				
			||||||
 | 
								var p float32
 | 
				
			||||||
 | 
								dst, err := os.OpenFile(part.FileName(), os.O_WRONLY|os.O_CREATE, 0644)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// so now loop through every appended file and upload
 | 
				
			||||||
 | 
								buffer := make([]byte, 100000)
 | 
				
			||||||
 | 
								for {
 | 
				
			||||||
 | 
									cBytes, err := part.Read(buffer)
 | 
				
			||||||
 | 
									if cBytes > 0 {
 | 
				
			||||||
 | 
										read = read + int64(cBytes)
 | 
				
			||||||
 | 
										//fmt.Printf("read: %v \n",read )
 | 
				
			||||||
 | 
										p = float32(read) / float32(length) * 100
 | 
				
			||||||
 | 
										fmt.Printf("progress: %v \n", p)
 | 
				
			||||||
 | 
										dst.Write(buffer[0:cBytes])
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if err == io.EOF {
 | 
				
			||||||
 | 
										fmt.Println(cBytes)
 | 
				
			||||||
 | 
										fmt.Println("eof this file")
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ctx.Text("finished")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
import React from 'react';
 | 
					import React, {BaseSyntheticEvent, useState} from 'react';
 | 
				
			||||||
import style from './MovieSettings.module.css';
 | 
					import style from './MovieSettings.module.css';
 | 
				
			||||||
import {APINode, callAPI} from '../../utils/Api';
 | 
					import {APINode, callAPI} from '../../utils/Api';
 | 
				
			||||||
import {GeneralSuccess} from '../../types/GeneralTypes';
 | 
					import {GeneralSuccess} from '../../types/GeneralTypes';
 | 
				
			||||||
@@ -123,6 +123,7 @@ class MovieSettings extends React.Component<Props, state> {
 | 
				
			|||||||
                        </div>
 | 
					                        </div>
 | 
				
			||||||
                    ))}
 | 
					                    ))}
 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
 | 
					                <FileHandler />
 | 
				
			||||||
            </>
 | 
					            </>
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -162,3 +163,47 @@ class MovieSettings extends React.Component<Props, state> {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default MovieSettings;
 | 
					export default MovieSettings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const FileHandler = (): JSX.Element => {
 | 
				
			||||||
 | 
					    const [file, setfile] = useState<File | null>(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const uploadFile = (): void => {
 | 
				
			||||||
 | 
					        let xhr = new XMLHttpRequest(); // create XMLHttpRequest
 | 
				
			||||||
 | 
					        let data = new FormData(); // create formData object
 | 
				
			||||||
 | 
					        if (!file) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        data.append('file', file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xhr.onload = function (): void {
 | 
				
			||||||
 | 
					            console.log(this.responseText); // whatever the server returns
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xhr.upload.onprogress = function (e): void {
 | 
				
			||||||
 | 
					            console.log(e.loaded / e.total);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xhr.open('post', '/api/video/fileupload'); // open connection
 | 
				
			||||||
 | 
					        xhr.setRequestHeader('Accept', 'multipart/form-data');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xhr.send(data); // send data
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return (
 | 
				
			||||||
 | 
					        <div style={{backgroundColor: 'white'}}>
 | 
				
			||||||
 | 
					            <input
 | 
				
			||||||
 | 
					                onChange={(e: BaseSyntheticEvent): void => {
 | 
				
			||||||
 | 
					                    console.log(e);
 | 
				
			||||||
 | 
					                    if (e.target) {
 | 
				
			||||||
 | 
					                        console.log(e.target.files[0]);
 | 
				
			||||||
 | 
					                        setfile(e.target.files[0]);
 | 
				
			||||||
 | 
					                        // setfile(e.target.files[0]);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }}
 | 
				
			||||||
 | 
					                type='file'
 | 
				
			||||||
 | 
					                name='Select file to upload'
 | 
				
			||||||
 | 
					            />
 | 
				
			||||||
 | 
					            <button onClick={(): void => uploadFile()}>upload</button>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user