Skip to content

Commit

Permalink
Complete Restrictions API
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedmcse committed Aug 27, 2024
1 parent d320b6d commit 970d4a2
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 5 deletions.
6 changes: 3 additions & 3 deletions waifuAPIModule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end function clearRestrictions
subroutine checkRestrictions(fileObj)
type(file_upload) :: fileObj
character(len=:), allocatable :: fullfilename, value, filemime
character(len=:), allocatable :: fullfilename, value, filemime, ext
integer :: rc, iostatus, filesize, maxfilesize, i
if (len_trim(fileObj%url) == 0) then
Expand All @@ -117,8 +117,8 @@ subroutine checkRestrictions(fileObj)
stop "RESTRICTION EXCEPTION: File " // fullfilename // " size greater than server maximum"
end if
elseif (trim(restrictions%restrictions(i)%type) == "BANNED_MIME_TYPE") then
! TODO Implement Mime Type Check
filemime = "application/x-dosexec"
ext = extension(trim(fileObj%filename))
filemime = getMime(ext)
value = trim(restrictions%restrictions(i)%value)
if (index(value, filemime) > 0) then
stop "RESTRICTION EXCEPTION: File " // fullfilename // " file type " // filemime // " banned on server"
Expand Down
177 changes: 176 additions & 1 deletion waifuUtilsModule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module waifuvault_utils
use, intrinsic :: iso_fortran_env
implicit none
private
public :: stringToLogical, basename, expandHomedir, getHomeDirectory, split_string, remove_characters
public :: stringToLogical, basename, expandHomedir, extension, getHomeDirectory, split_string, remove_characters, getMime

contains

Expand All @@ -29,6 +29,23 @@ function basename(path)
basename = path
end function basename

function extension(pathname) result(ext)
character(len=*), intent(in) :: pathname
character(len=:), allocatable :: ext
integer :: i

i = len_trim(pathname)
do while (i > 0)
if (pathname(i:i) == '.') then
ext = pathname(i:)
return
end if
i = i - 1
end do

ext = ''
end function extension

subroutine expandHomedir(path, result)
character(len=*), intent(in) :: path
character(len=:), allocatable :: result
Expand Down Expand Up @@ -124,4 +141,162 @@ subroutine remove_characters(original_string, chars_to_remove, new_string)

new_string = trim(temp_string)
end subroutine remove_characters

function getMime(ext) result (mime)
character(len=*) :: ext
character(len=:), allocatable :: mime
! List from https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

select case (trim(ext))
case ('.aac')
mime = 'audio/aac'
case ('.abw')
mime = 'application/x-abiword'
case ('.arc')
mime = 'application/x-freearc'
case ('.avif')
mime = 'image/avif'
case ('.avi')
mime = 'video/x-msvideo'
case ('.azw')
mime = 'application/vnd.amazon.ebook'
case ('.bin')
mime = 'application/octet-stream'
case ('.bmp')
mime = 'image/bmp'
case ('.bz')
mime = 'application/x-bzip'
case ('.bz2')
mime = 'application/x-bzip2'
case ('.cda')
mime = 'application/x-cdf'
case ('.csh')
mime = 'application/x-csh'
case ('.css')
mime = 'text/css'
case ('.csv')
mime = 'text/csv'
case ('.doc')
mime = 'application/msword'
case ('.docx')
mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
case ('.eot')
mime = 'application/vnd.ms-fontobject'
case ('.epub')
mime = 'application/epub+zip'
case ('.exe')
mime = 'application/x-dosexec'
case ('.gz')
mime = 'application/gzip'
case ('.gif')
mime = 'image/gif'
case ('.htm', '.html')
mime = 'text/html'
case ('.ico')
mime = 'image/vnd.microsoft.icon'
case ('.ics')
mime = 'text/calendar'
case ('.jar')
mime = 'application/java-archive'
case ('.jpeg', '.jpg')
mime = 'image/jpeg'
case ('.js')
mime = 'text/javascript'
case ('.json')
mime = 'application/json'
case ('.jsonld')
mime = 'application/ld+json'
case ('.mid', '.midi')
mime = 'audio/midi'
case ('.mjs')
mime = 'text/javascript'
case ('.mp3')
mime = 'audio/mpeg'
case ('.mp4')
mime = 'video/mp4'
case ('.mpeg')
mime = 'video/mpeg'
case ('.mpkg')
mime = 'application/vnd.apple.installer+xml'
case ('.odp')
mime = 'application/vnd.oasis.opendocument.presentation'
case ('.ods')
mime = 'application/vnd.oasis.opendocument.spreadsheet'
case ('.odt')
mime = 'application/vnd.oasis.opendocument.text'
case ('.oga')
mime = 'audio/ogg'
case ('.ogv')
mime = 'video/ogg'
case ('.ogx')
mime = 'application/ogg'
case ('.opus')
mime = 'audio/opus'
case ('.otf')
mime = 'font/otf'
case ('.png')
mime = 'image/png'
case ('.pdf')
mime = 'application/pdf'
case ('.php')
mime = 'application/x-httpd-php'
case ('.ppt')
mime = 'application/vnd.ms-powerpoint'
case ('.pptx')
mime = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
case ('.rar')
mime = 'application/vnd.rar'
case ('.rtf')
mime = 'application/rtf'
case ('.sh')
mime = 'application/x-sh'
case ('.svg')
mime = 'image/svg+xml'
case ('.tar')
mime = 'application/x-tar'
case ('.tif', '.tiff')
mime = 'image/tiff'
case ('.ts')
mime = 'video/mp2t'
case ('.ttf')
mime = 'font/ttf'
case ('.txt')
mime = 'text/plain'
case ('.vsd')
mime = 'application/vnd.visio'
case ('.wav')
mime = 'audio/wav'
case ('.weba')
mime = 'audio/webm'
case ('.webm')
mime = 'video/webm'
case ('.webp')
mime = 'image/webp'
case ('.woff')
mime = 'font/woff'
case ('.woff2')
mime = 'font/woff2'
case ('.xhtml')
mime = 'application/xhtml+xml'
case ('.xls')
mime = 'application/vnd.ms-excel'
case ('.xlsx')
mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
case ('.xml')
mime = 'application/xml'
case ('.xul')
mime = 'application/vnd.mozilla.xul+xml'
case ('.zip')
mime = 'application/zip'
case ('.3gp')
mime = 'video/3gpp'
case ('.3g2')
mime = 'video/3gpp2'
case ('.7z')
mime = 'application/x-7z-compressed'
case default
mime = 'application/octet-stream'
end select
end function getMime

end module waifuvault_utils
2 changes: 1 addition & 1 deletion waifurestrictiontest.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ program test_waifuvault_restrictions
call sleep(1)

! Upload Normal File
call realfile_upload%create_upload('~/Downloads/rider3.png', '', '10m', '', .false., .false.)
call realfile_upload%create_upload('~/Dropbox/Public/filebundler.exe', '', '10m', '', .false., .false.)
realfile_response = uploadFile(realfile_upload)
print *, '--File Upload Response--'
print *, 'Token:', trim(realfile_response%token)
Expand Down

0 comments on commit 970d4a2

Please sign in to comment.